I'm in trouble with a problem of the Java JSpinner. If I do not modify the value which is displayed in the spinner everything works. But if I change the value (a date) using the arrows my program returns to me the date of 1th January 1970.
Here's the snippet of code:
package main;
import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class GUI {
JSpinner mySpinner;
public void createGUI(){
JFrame frame=new JFrame();
JPanel panel=new JPanel(new FlowLayout());
panel.setSize(300,100);
mySpinner=new JSpinner();
mySpinner.setModel(new SpinnerDateModel(new Date(), null, null, Calendar.MINUTE));
mySpinner.setEditor(new JSpinner.DateEditor(mySpinner, "HH:mm"));
mySpinner.setBounds(0, 0, 71, 28);
final JLabel myLabel=new JLabel();
panel.add(myLabel);
mySpinner.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("JSpinner: "+mySpinner.getValue());
}
});
frame.add(panel);
panel.add(mySpinner);
frame.setVisible(true);
}
}
This is the main:
public static void main(String[] args) {
// TODO Auto-generated method stub
GUI myGUI=new GUI();
myGUI.createGUI();
}
And this is what console prints:
JSpinner: Thu Jan 01 23:03:00 CET 1970
JSpinner: Thu Jan 01 22:03:00 CET 1970