Search code examples
javatimerjtextfield

perform task at a user defined time


I need to get a time from the user and schedule my task at that particular time. For getting the time from the user, I displayed the current system time in a JTextField so that the user can edit it easily if wanted.

private JTextField time;    
String date = new SimpleDateFormat("hh:mm:ss").format(new Date());
            time = new JTextField(date);
            time.setBounds(452, 251, 200, 20);
            frame.getContentPane().add(time);
            time.setColumns(10);

But if i do this, I am unable to extract the modified time back in my code. If I extract it by using time.getText() it is in the string format. and i cannot use it in my scheduler

Is there any other way of getting a user defined time in the date format itself?

Also, what is the proper way of delaying my task till the specified time? Its a one-time task, not a repetitive task.


Solution

  • Use a SpinnerDateModel in a JSpinner.

    Spinner demo

    For more information see How to Use Spinners.