java - Using multiple JComboBoxes; but they all get any action events -
i'm sure i'm doing stupid here; it's been bugging me hours. i'm using netbeans 8.something, if helps. have form multiple combo boxes, select data set. first selects year, choice populates monthcombo, selection populates daycombo, , on.
the automatically generated code here:
javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); jpanel1.setlayout(jpanel1layout); jpanel1layout.sethorizontalgroup( jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jscrollpane1, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 1323, short.max_value) .addgroup(jpanel1layout.createsequentialgroup() .addcontainergap() .addcomponent(yearcombo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(monthcombo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) .addcomponent(daycombo, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) );
i've given them each different actionlistener, so:
yearcombo.addactionlistener(yearlistener); monthcombo.addactionlistener(monthlistener); daycombo.addactionlistener(daylistener);
where listeners defined so:
// ------------------------------------------------------- private actionlistener daylistener=new actionlistener(){ @override public void actionperformed(actionevent e) { if(e.getmodifiers()==clicked){ log("day combo item "+daycombo.getselecteditem().tostring()+" source "+e.getsource().tostring()); rdataselector.set(rdataselector.select_day,integer.parseint(daycombo.getselecteditem().tostring())); rdataselector.fillcombo(coursecombo); } } }; // --------------------------------------------------------------- private actionlistener monthlistener=new actionlistener(){ @override public void actionperformed(actionevent e) { if(e.getmodifiers()==clicked){ log("month combo item "+monthcombo.getselecteditem().tostring()+" source "+e.getsource().tostring()); rdataselector.set(rdataselector.select_month,integer.parseint(monthcombo.getselecteditem().tostring())); rdataselector.fillcombo(daycombo); } } }; // ----------------------------------------------------------------- private actionlistener yearlistener=new actionlistener(){ @override public void actionperformed(actionevent e) { if(e.getmodifiers()==clicked){ log("year combo item "+yearcombo.getselecteditem().tostring()); rdataselector.set(rdataselector.select_year,integer.parseint(yearcombo.getselecteditem().tostring())); rdataselector.fillcombo(monthcombo); } } };
fwiw, clicked defined 16, since that's event modifier left mouse click.
the problem if select, example, item yearcombo, 3 actionlisteners called.
this output log() when year item 2015 clicked.
year combo item 2015
month combo item 2015 source javax.swing.jcombobox[,77,6,37x25,invalid,layout=javax.swing.plaf.basic.basiccomboboxui$handler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.synth.synthborder@1b2a5dc,flags=320,maximumsize=,minimumsize=,preferredsize=,iseditable=false,lightweightpopupenabled=true,maximumrowcount=8,selecteditemreminder=2015]
day combo item 2015 source javax.swing.jcombobox[,120,6,37x25,invalid,layout=javax.swing.plaf.basic.basiccomboboxui$handler,alignmentx=0.0,alignmenty=0.0,border=javax.swing.plaf.synth.synthborder@f77fca,flags=320,maximumsize=,minimumsize=,preferredsize=,iseditable=false,lightweightpopupenabled=true,maximumrowcount=8,selecteditemreminder=2015]
i know can source of event, doesn't help, there's no obvious way tell them apart.
i can't believe meant work this, doing wrong?
on select in year combo fill month combo event fired. accordingly selection changed daycombo populated firing event.
there 2 approachs:
define flag isapi. default it's false. if logic updates combo set true nd t false after items update. listeners check flag , it's true (api call) nothing.
remove th listeners combox before updating models , readd after model populated
Comments
Post a Comment