java - JSpinner with SpinnerDateModel not spinning -
i'm developing java application using swing create gui. have problem jspinner, whih should allow user select time, in hh:mm format, specific range. created custom spinnerdatemodel , dateeditor , seems ok, when run application spinner shows proper value doesn't spinn.
import java.util.calendar; import java.util.date; import javax.swing.jspinner; import javax.swing.spinnerdatemodel; public class spinnerexample extends javax.swing.jframe { public static void main(string[] args) { spinnerexample main = new spinnerexample(); main.setvisible(true); } /** * creates new form mainframe */ public spinnerexample() { initcomponents(); myspinner.setmodel(getmyspinnermodel()); myspinner.seteditor(new jspinner.dateeditor(myspinner, "hh:mm")); } public spinnerdatemodel getmyspinnermodel() { spinnerdatemodel spinnermodel = new spinnerdatemodel(); calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, 12); calendar.set(calendar.minute, 0); date selecteddate = calendar.gettime(); calendar.set(calendar.hour_of_day, 23); date enddate = calendar.gettime(); calendar.set(calendar.hour_of_day, 1); date startdate = calendar.gettime(); spinnermodel = new spinnerdatemodel(selecteddate, startdate, enddate, calendar.hour_of_day); return spinnermodel; } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { myspinner = new javax.swing.jspinner(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(176, 176, 176) .addcomponent(myspinner, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap(195, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(106, 106, 106) .addcomponent(myspinner, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap(174, short.max_value)) ); pack(); }// </editor-fold> // variables declaration - not modify private javax.swing.jspinner myspinner; // end of variables declaration }
when put nulls "start" , "end" parameters while creating spinnermodel below:
spinnermodel = new spinnerdatemodel(selecteddate, null, null, calendar.hour_of_day);
spinner works well.
what can wrong here?
i same strange behavior , can't find reasonable explanation. however, works using custom editor class below. solve problem?
import java.awt.event.actionevent; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import javax.swing.jformattedtextfield; import javax.swing.jframe; import javax.swing.jspinner; import javax.swing.spinnerdatemodel; import javax.swing.event.changeevent; import javax.swing.event.changelistener; import javax.swing.text.dateformatter; import javax.swing.text.defaultformatterfactory; public class spinnerexample extends jframe { private final jspinner myspinner; public static void main(string[] args) { spinnerexample main = new spinnerexample(); } public spinnerexample() { super("spinnerexample"); myspinner = new jspinner(); myspinner.setmodel(getmyspinnermodel()); // myspinner.seteditor(new jspinner.dateeditor(myspinner, "hh:mm")); myspinner.seteditor(new customeditor(myspinner, "hh:mm")); add(myspinner); pack(); setvisible(true); setdefaultcloseoperation(exit_on_close); } private spinnerdatemodel getmyspinnermodel() { calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, 10); calendar.set(calendar.minute, 0); calendar.set(calendar.second, 0); date selecteddate = calendar.gettime(); calendar.set(calendar.hour_of_day, 23); date enddate = calendar.gettime(); calendar.set(calendar.hour_of_day, 0); date startdate = calendar.gettime(); return new spinnerdatemodel(selecteddate, startdate, enddate, calendar.hour); } } class customeditor extends jformattedtextfield implements changelistener { private final jspinner spinner; public customeditor(jspinner spinner, string pattern) { super(); this.spinner = spinner; dateformatter formatter = new dateformatter(new simpledateformat(pattern)); formatter.setallowsinvalid(false); formatter.setoverwritemode(true); setformatterfactory(new defaultformatterfactory(formatter)); updatevalue(); spinner.addchangelistener(this); addactionlistener((actionevent e) -> { this.spinner.getmodel().setvalue(this.getvalue()); }); } @override public void statechanged(changeevent e) { updatevalue(); } private void updatevalue() { setvalue((date) spinner.getmodel().getvalue()); } }
Comments
Post a Comment