Tuesday, 15 February 2011

java - Design time: How to assign to multiple Swing Controls the same event? -



java - Design time: How to assign to multiple Swing Controls the same event? -

i have 10 jsliders, 10 jlabels , 10 jtogglebuttons. when user drag slider, need update corresponding label , move same value sliders corresponding button toggled. now, problem not how this, instead how have in same event.

in ol' vb days ( ;) ) i'd have assing sliders same name, , buttons , labels, , i'd have automagically arrays of sliders, labels , buttons accessible index, firing same event.

in current c++builder days, have set tag property (a generic int property) each of them, select sliders , declare same event them all, tag sender object , utilize index. i'll declare array of labels , buttons pointers, can access command index.

in java, how can set same event sliders in ide (i utilize netbeans)? , how can access labels , togglebuttons array?

i've seens illustration building controls @ runtime, want design form way, instead.

thanks

ok, fast, here had in mind (can much improved, used shortcuts faster):

import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.beans.propertychangesupport; import java.util.arraylist; import java.util.list; import java.util.random; import javax.swing.abstractbutton; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jslider; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class testmvc { public static class modelobject { private propertychangesupport pcs; public modelobject() { pcs = new propertychangesupport(this); } public propertychangesupport getpropertychangesupport() { homecoming pcs; } } public static class model extends modelobject { private list<item> items; public model() { items = new arraylist<testmvc.item>(); } public list<item> getitems() { homecoming items; } public void addtoitems(item item) { item.setmodel(this); items.add(item); getpropertychangesupport().firepropertychange("item", null, item); } public void removefromitems(item item) { items.add(item); item.setmodel(null); getpropertychangesupport().firepropertychange("item", item, null); } } public static class item extends modelobject { public static final string value = "value"; public static final string label = "label"; public static final string enabled = "enabled"; private model model; private boolean enabled; private string label; private int value; public item() { } public item(boolean enabled, string label, int value) { super(); this.enabled = enabled; this.label = label; this.value = value; } public model getmodel() { homecoming model; } public void setmodel(model model) { this.model = model; } public boolean isenabled() { homecoming enabled; } public void setenabled(boolean enabled) { boolean old = this.enabled; this.enabled = enabled; getpropertychangesupport().firepropertychange(enabled, old, enabled); } public string getlabel() { homecoming label; } public void setlabel(string label) { string old = this.label; this.label = label; getpropertychangesupport().firepropertychange(label, old, enabled); } public int getvalue() { homecoming value; } public void setvalue(int value) { _internalsetvalue(value); if (this.isenabled()) { (item item : model.getitems()) { if (item != && item.isenabled()) { item._internalsetvalue(value); } } } } private void _internalsetvalue(int value) { int old = this.value; this.value = value; getpropertychangesupport().firepropertychange(value, old, value); } } public static class itemview implements propertychangelistener { private item item; private jlabel label; private jslider slider; private abstractbutton button; public itemview(item item) { super(); this.label = new jlabel(item.getlabel()); this.slider = new jslider(0, 100); this.slider.setvalue(item.getvalue()); this.slider.setenabled(item.isenabled()); this.button = new jcheckbox((string) null, item.isenabled()); this.item = item; item.getpropertychangesupport().addpropertychangelistener(this); } public jlabel getlabel() { homecoming label; } public jslider getslider() { homecoming slider; } public abstractbutton getbutton() { homecoming button; } @override public void propertychange(propertychangeevent evt) { if (evt.getpropertyname().equals(item.label)) { label.settext(item.getlabel()); } else if (evt.getpropertyname().equals(item.enabled)) { button.setselected(item.isenabled()); slider.setenabled(item.isenabled()); } else if (evt.getpropertyname().equals(item.value)) { slider.setvalue(item.getvalue()); } } } private model model; public testmvc(model model) { super(); this.model = model; } protected void initui() { jframe frame = new jframe(testmvc.class.getsimplename()); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(new gridbaglayout()); gridbagconstraints gbc1 = new gridbagconstraints(); gridbagconstraints gbc3 = new gridbagconstraints(); gbc3.weightx = 1.0; gbc3.fill = gridbagconstraints.horizontal; gbc3.gridwidth = gridbagconstraints.remainder; (final item item : model.getitems()) { final itemview view = new itemview(item); view.getbutton().addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { item.setenabled(view.getbutton().isselected()); } }); view.getslider().addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { item.setvalue(view.getslider().getvalue()); } }); panel.add(view.getbutton(), gbc1); panel.add(view.getlabel(), gbc1); panel.add(view.getslider(), gbc3); } frame.add(panel); frame.pack(); frame.setvisible(true); } private static model loadmodel() { random random = new random(); model model = new model(); (int = 0; < 10; i++) { item item = new item(random.nextboolean(), "some label " + random.nextlong(), random.nextint(100)); model.addtoitems(item); } homecoming model; } public static void main(string[] args) { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (instantiationexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (illegalaccessexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (unsupportedlookandfeelexception e) { // todo auto-generated grab block e.printstacktrace(); } swingutilities.invokelater(new runnable() { @override public void run() { model model = loadmodel(); new testmvc(model).initui(); } }); } }

java arrays swing events

No comments:

Post a Comment