java - How to get the values from JPanel components like drop down, text fields which are getting created on a fly -
i have jpanel
consists of dropdown , text field inside jframe
. there button in jframe
, when user clicks on button, application adds new jpanel
same components i.e. drop down , text field. so, have created function gets called on clicking on button using actionlistener
.
everything works fine gui side problem when user done adding jpanel
s , entering values in these drop downs , text fields, click on submit button. upon clicking on submit button, should able fetch values drop downs , text fields. challenge, since using same functions create jpanel
s, can't call name values since give me last jpanel
values.
any suggestion how should go this? have added screenshot of jframe
, function create jpanel
. appreciated. thanks.
public static void addpanel(final container pane) { panel1 = new jpanel(); string text = "<html><b>property" + nooftimes + " :</b></html>"; jlabel label = new jlabel(text); label.setpreferredsize(new dimension(80, 30)); panel1.add(label); panel1.add(new jlabel("please enter property")); defaultcomboboxmodel<string> model = new defaultcomboboxmodel<string>(); model.addelement("value1"); model.addelement("value2"); model.addelement("value3"); model.addelement("value4"); model.addelement("value5"); final jcombobox<string> combobox1 = new jcombobox<string>(model); autocompletedecorator.decorate(combobox1); combobox1.setpreferredsize(new dimension(120, 22)); panel1.add(combobox1); final jtextfield txtfield1 = new jtextfield( "please enter value here"); txtfield1.setpreferredsize(new dimension(200, 22)); panel1.add(txtfield1); txtfield1.addfocuslistener(new focuslistener() { public void focusgained(focusevent e) { txtfield1.settext(""); } public void focuslost(focusevent e) { // nothing } }); container.add(panel1); nooftimes++; frame.revalidate(); frame.validate(); frame.repaint(); }
you return jpanel
, store in list<jpanel>
. when click submit-button able iterate through jpanel
s , component
s.
public class application { private static list<jpanel> panels = new arraylist<>(); private static container somecontainer = new container(); public static void main(string[] args) { panels.add(addpanel(somecontainer)); panels.add(addpanel(somecontainer)); panels.add(addpanel(somecontainer)); submit(); } public static jpanel addpanel(final container pane) { jpanel panel1 = new jpanel(); // shortened code final jcombobox<string> combobox1 = new jcombobox<string>(); panel1.add(combobox1); final jtextfield txtfield1 = new jtextfield("please enter value here"); txtfield1.settext(string.valueof(math.random())); panel1.add(txtfield1); return panel1; } private static void submit() { (jpanel panel : panels) { component[] components = panel.getcomponents(); (component comp : components) { // cast comp jcombobox / jtextfield values if (comp instanceof jtextfield) { jtextfield textfield = (jtextfield) comp; system.out.println(textfield.gettext()); } } } } }
Comments
Post a Comment