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
...
}
}
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));