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
                ...
        }
    }
Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
yetin
  • 61
  • 11

1 Answers1

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));
blackbishop
  • 30,945
  • 11
  • 55
  • 76
  • This works man ! Thank you loads :) If you don't mind, can you have a look at this question ? :/ http://stackoverflow.com/questions/29644685/retrieve-image-from-jlabel-and-store-it-in-a-variable-in-java – yetin Apr 15 '15 at 15:24
  • Sorry to bother you again.. What if I want to clear or reset the spinner to 00:00:00 ? How do I achieve that? – yetin Apr 15 '15 at 15:31
  • Create `String reset = "00:00:00"`, format it and set the value as shown in the code above. – blackbishop Apr 15 '15 at 15:41