0

How do I retrieve the time format from a MySQL database and use it to populate a JSpinner in Java? The JSpinner should be populated when I click the 'NEXT' button. The code I am currently using is as follows:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 24); // 24 == 12 PM == 00:00:00
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    SpinnerDateModel model = new SpinnerDateModel();
    model.setValue(calendar.getTime());

    spinner = new JSpinner(model);
    JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "HH:mm:ss");
    DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
    formatter.setAllowsInvalid(false); 
    formatter.setOverwriteMode(true);

    spinner.setEditor(editor);
    spinner.setBounds(419, 218, 119, 26);
    frame.getContentPane().add(spinner);

    JButton btnNext = new JButton("Next");
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                try {
                    if(rs.next()){
                    String time = rs.getString("Time");
                    spinner.setValue(time); // This is not working
                ...
        }
    }

1 Answer 1

2

You are using SpinnerDateModel as a model for your JSpinner and it uses a Date type, but your are trying to set a String value.

I think you need to format it to Date before you set the value of your spinner :

String time = rs.getString("Time");
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
spinner.setValue(format.parseObject(time));
Sign up to request clarification or add additional context in comments.

3 Comments

This works man ! Thank you loads :) If you don't mind, can you have a look at this question ? :/ stackoverflow.com/questions/29644685/…
Sorry to bother you again.. What if I want to clear or reset the spinner to 00:00:00 ? How do I achieve that?
Create String reset = "00:00:00", format it and set the value as shown in the code above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.