java - update components based on which tab its called from -
hello guru's beginner java swing developer , trying build simple app... here simplified version.
setup: 1 jframe, 2 borderlayout based tabbed panels (a , b) each has 1 textfiled, shared jpanel class combo box , itemlistener initialized in each tab's (north)
issue: how control updates textfield's text based on panel came eg. if select apples in taba, item listener updates textfield on tabb well. accomplish determine call came taba or tabb , update textfield associated tab.
hope makes sense
public class main extends jframe { private static final long serialversionuid = 1l; private jpanel contentpane; public jtextfield tfpanela; public jtextfield tfpanelb; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { main frame = new main(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ public main() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 300); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); contentpane.setlayout(new borderlayout(0, 0)); setcontentpane(contentpane); jtabbedpane tabbedpane = new jtabbedpane(jtabbedpane.top); contentpane.add(tabbedpane, borderlayout.center); jpanel panela = new jpanel(); tabbedpane.addtab("tab a", null, panela, null); panela.setlayout(new borderlayout(0, 0)); sharedpanel s1 = new sharedpanel(this); panela.add(s1, borderlayout.north); tfpanela = new jtextfield(); panela.add(tfpanela); tfpanela.setcolumns(10); jpanel panelb = new jpanel(); tabbedpane.addtab("tab b", null, panelb, null); panelb.setlayout(new borderlayout(0, 0)); sharedpanel s2 = new sharedpanel(this); panelb.add(s2, borderlayout.north); tfpanelb = new jtextfield(); panelb.add(tfpanelb); tfpanelb.setcolumns(10); } }
// shared class...
public class sharedpanel extends jpanel { private main app; private string[] clist = {"apples","oranges","bananas"}; /** * create panel. */ public sharedpanel(final main app) { this.app=app; setlayout(new flowlayout(flowlayout.center, 5, 5)); jcombobox<string> combobox = new jcombobox<string>(); combobox.setmodel(new defaultcomboboxmodel<string>(clist)); combobox.additemlistener(new itemlistener() { public void itemstatechanged(itemevent e) { //update panela's textfield if called panela // if called panela app.tfpanela.settext(e.getitem().tostring()); // if called panelb app.tfpanelb.settext(e.getitem().tostring()); } }); add(combobox); } }
since have create new instance of sharedpanel
each tab, provide reference text field want updated it...
which end more like...
public class sharedpanel extends jpanel { private jtextfield field; private string[] clist = {"apples","oranges","bananas"}; public sharedpanel(final jtextfield field) { this.field=field; setlayout(new flowlayout(flowlayout.center, 5, 5)); jcombobox<string> combobox = new jcombobox<string>(); combobox.setmodel(new defaultcomboboxmodel<string>(clist)); combobox.additemlistener(new itemlistener() { public void itemstatechanged(itemevent e) { sharedpanel.this.field.settext(e.getitem().tostring()); } }); add(combobox); } }
updated "model" example
this basic example of 1 possible use of model bridge changes between common panel , other panels. means common panel doesn't care else , updates supplied model, fires events interested parties can take appropriate action required.
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.itemevent; import java.awt.event.itemlistener; import java.util.arraylist; import java.util.list; import javax.swing.defaultcomboboxmodel; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtabbedpane; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; public class tabbedmodel { protected static final string[] main_list = {"apples","oranges","bananas"}; public static void main(string[] args) { new tabbedmodel(); } public tabbedmodel() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jtabbedpane tabpane = new jtabbedpane(); tabpane.addtab("a", new atab()); tabpane.addtab("b", new atab()); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(tabpane); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class defaultcommonmodel implements commonmodel { private list<changelistener> listeners; private string value; public defaultcommonmodel() { listeners = new arraylist<>(25); } @override public void addchangelistener(changelistener listener) { listeners.add(listener); } @override public void removechangelistener(changelistener listener) { listeners.remove(listener); } @override public void setvalue(string avalue) { if (avalue == null ? value != null : !avalue.equals(value)) { value = avalue; firestatechanged(); } } @override public string getvalue() { return value; } protected void firestatechanged() { if (!listeners.isempty()) { changeevent evt = new changeevent(this); (changelistener listener : listeners) { listener.statechanged(evt); } } } } public interface commonmodel { public void addchangelistener(changelistener listener); public void removechangelistener(changelistener listener); public void setvalue(string value); public string getvalue(); } public class commonpanel extends jpanel { private commonmodel model; private jcombobox combobox; public commonpanel(commonmodel model) { this.model = model; setlayout(new gridbaglayout()); combobox = new jcombobox(new defaultcomboboxmodel<>(main_list)); combobox.additemlistener(new itemlistener() { @override public void itemstatechanged(itemevent e) { string value = (string) e.getitem(); commonpanel.this.model.setvalue(value); } }); add(combobox); } } public class atab extends jpanel { private list<jtextfield> fields; public atab() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; defaultcommonmodel model = new defaultcommonmodel(); model.addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { commonmodel model = (commonmodel) e.getsource(); (jtextfield field : fields) { field.settext(model.getvalue()); } } }); commonpanel commonpanel = new commonpanel(model); add(commonpanel, gbc); fields = new arraylist<>(25); (int index = 0; index < random(); index++) { jtextfield field = new jtextfield(10); add(field, gbc); fields.add(field); } } protected int random() { return (int)math.round(math.random() * 9) + 1; } } }
Comments
Post a Comment