java - Action listener on comboBox to display selection in textField -
i have combobox contains selections. trying apply actionlistener display te selected string on textfield. not sure how complete , have read confusing me more.
here code combobox:
jcombobox combobox = new jcombobox(); combobox.setmodel(new defaultcomboboxmodel(new string[] {"select something", "selection 1", "selection 2", "selection 3", "selection 4"})); paneltop.add(combobox); combobox.setpreferredsize(new dimension(160, 20)); combobox.additem("select something");
textfield code:
textfield = new jtextfield(); paneltop.add(textfield); textfield.setcolumns(40);
what have far on actionlistener:
public void actionperformed(actionevent e) { jcombobox combobox = (jcombobox)e.getsource(); string newselection = (string)combobox.getselecteditem(); currentpattern = newselection; }
full code:
package swingassign2; import java.awt.dimension; import java.awt.eventqueue; import javax.swing.borderfactory; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextpane; import java.awt.borderlayout; import javax.swing.jcombobox; import javax.swing.jtextfield; import java.awt.flowlayout; import javax.swing.swingconstants; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.itemevent; import java.awt.event.itemlistener; import javax.swing.defaultcomboboxmodel; public class swingassign2 { private jframe frame; private jtextfield textfield; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { swingassign2 window = new swingassign2(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create application. */ public swingassign2() { initialize(); } /** * initialize contents of frame. */ private void initialize() { frame = new jframe(); frame.setsize(600, 500); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel paneltop = new jpanel(); flowlayout fl_paneltop = (flowlayout) paneltop.getlayout(); fl_paneltop.setalignment(flowlayout.left); frame.getcontentpane().add(paneltop, borderlayout.north); //panel.setlayout(new flowlayout(flowlayout.left, 5, 5)); final jcombobox combobox = new jcombobox(); combobox.setmodel(new defaultcomboboxmodel(new string[] {"select something", "selection 1", "selection 2", "selection 3", "selection 4"})); paneltop.add(combobox); combobox.setpreferredsize(new dimension(160, 20)); combobox.additem("select something"); textfield = new jtextfield(); paneltop.add(textfield); textfield.setcolumns(40); jpanel panelbottom = new jpanel(); frame.getcontentpane().add(panelbottom, borderlayout.south); panelbottom.setlayout(new borderlayout(0, 0)); jlabel status = new jlabel("status"); status.sethorizontalalignment(swingconstants.center); panelbottom.add(status, borderlayout.south); status.setborder(borderfactory.createloweredbevelborder()); jtextpane txtpnsomecontenton_b = new jtextpane(); txtpnsomecontenton_b.settext("some content on bottom panel\r\nmore content\r\nsome more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content"); jscrollpane jscrollpane3 = new jscrollpane(txtpnsomecontenton_b); jscrollpane3.sethorizontalscrollbarpolicy( jscrollpane.horizontal_scrollbar_always); jscrollpane3.setverticalscrollbarpolicy( jscrollpane.vertical_scrollbar_always); panelbottom.add(jscrollpane3, borderlayout.north); jscrollpane3.setpreferredsize(new dimension(200, 200)); //btext.setborder(borderfactory.createloweredbevelborder()); jpanel panelcenter = new jpanel(); frame.getcontentpane().add(panelcenter, borderlayout.center); panelcenter.setlayout(new gridlayout(0, 2, 0, 0)); jtextpane txtpnsomecontenton_0 = new jtextpane(); txtpnsomecontenton_0.settext("some content on left panel\r\nmore content\r\nsome more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content"); jscrollpane jscrollpane1 = new jscrollpane(txtpnsomecontenton_0); jscrollpane1.sethorizontalscrollbarpolicy( jscrollpane.horizontal_scrollbar_always); jscrollpane1.setverticalscrollbarpolicy( jscrollpane.vertical_scrollbar_always); panelcenter.add(jscrollpane1); jtextpane txtpnsomecontenton = new jtextpane(); txtpnsomecontenton.settext("some content on right panel\r\nmore content\r\nsome more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content\r\nmore , more content"); jscrollpane jscrollpane2 = new jscrollpane(txtpnsomecontenton); jscrollpane2.sethorizontalscrollbarpolicy( jscrollpane.horizontal_scrollbar_always); jscrollpane2.setverticalscrollbarpolicy( jscrollpane.vertical_scrollbar_always); panelcenter.add(jscrollpane2); combobox.additemlistener(new itemlistener() { @override public void itemstatechanged(itemevent e) { if(e.getstatechange() == itemevent.selected) { textfield.settext((string) combobox.getselecteditem()); } } }); } }
consider adding itemlistener
jcombobox
, checking itemevent
state itemevent.selected
. can set text field's text in itemstatechanged
method:
combobox.additemlistener(new itemlistener() { @override public void itemstatechanged(itemevent e) { if(e.getstatechange() == itemevent.selected) { textfield.settext((string) combobox.getselecteditem()); } } });
Comments
Post a Comment