Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:15:46+00:00 2026-06-09T12:15:46+00:00

In one of my java applications, there is a field where the user is

  • 0

In one of my java applications, there is a field where the user is supposed to enter a time. I know, I can simply keep it as a normal JTextField and at the time of validation I can check the value and so on…

But I think there is a better way than that..

For example, if this is a matter with a date, then JXDatePicker (which comes with swingx) can be used very handily. Eventually, the users selection is guaranteed to be a date.

Likewise, there must be a better way for time selection too. Will someone share your knowledge if you know a better solution.

Any thought is appreciated. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:15:47+00:00Added an answer on June 9, 2026 at 12:15 pm

    you could use a JFormattedTextField have a look at the example below, this will create a JFormattedTextField which will accept only numbers and put them in the form XXhXXminXXs then add a ActionListener to the JFormattedTextField and in that attempt to parse to a valid time object when ENTER for now I only show the use of the JFormattedTextField and the ActionListener I didn’t attempt to convert or any converting and parsing for valid time:

    enter image description here

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.ParseException;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.text.MaskFormatter;
    
    public class FormattedTextFieldExample {
    
        public FormattedTextFieldExample() {
            initComponents();
        }
    
        private void initComponents() {
            JFrame frame = new JFrame("JFormattedTextField Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            MaskFormatter mask = null;
            try {
                mask = new MaskFormatter("##h##min##s");//the # is for numeric values
                mask.setPlaceholderCharacter('#');
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            //
            // Create a formatted text field that accept a valid time.
            //
            final JFormattedTextField timeField = new JFormattedTextField(mask);
    
            //Add ActionListener for when enter is pressed
            timeField.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    Object source = ae.getSource();
                    if (source == timeField) {
                        //parse to a valid time here
                        System.out.println(timeField.getText());
                    }
                }
            });
    
            frame.add(timeField);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new FormattedTextFieldExample();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two separated applications, one written in Java and other in Erlang. Both
I noticed in two applications to generate hashes of files, one written in Java
In one of my Java EE applications, I used a registration page to register
I am trying to integrate facebook,twitter,linkedin chat in to one java application. I was
I'm preparing to port one Java Swing application to the web. The application is
On one of our Java application screens, we want to display data loaded from
I have a Java application which requires certain software (one of them being Perl)
I'm writing a Java application with GUI using Swing. One of the GUI components
So i am writing my first Java application using MVP pattern. One of MVPs
I have built a web application using Java EE platform that sells one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.