I want to set my JSpinner in Neatbeans swing with the current time, and allow user to choose time through clicking the arrows up and down. I have these code which somehow works at first but cannot change the time when clicking the arrows. Here's my code:
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerDateModel());
mySpinnerControl.setEditor(new JSpinner.DateEditor(spinner, "HH:mm"));
When i click the arrow keys, time wont change and got this error message:
java.lang.ClassCastException: javax.swing.SpinnerNumberModel cannot be cast to javax.swing.SpinnerDateModel
What is wrong with my code? I researched through net and they all have the same code. Any help would be greatly appreciated. Thanks ahead.
What is
mySpinnerControl
? I could get your example to work when I changedmySpinnerControl
tospinner
. My guess is that you set the editor on another component, and thusspinner
still uses a Number-Editor which then produces the exception when it tries to set the date/time value.The following code worked for me: