java - get access to each dynamically added jComponent -
before asked question , value dynamically added jtextfields
, jcomboboxes
using answer question.
now in subpanel
have 3 jcomboboxes
, 4 jtextfields
.
to value of jcomponent
using code:
component[] children = jpanel1.getcomponents(); // iterate on subpanels... (component sp : children) { if (sp instanceof subpanel) { component[] spchildren = ((subpanel)sp).getcomponents(); // iterate on jtextfields... (component spchild : spchildren) { if (spchild instanceof jtextfield) { string text = ((jtextfield)spchild).gettext(); system.out.println(text); } } } }
i ask possible access each jcomboboxes
, jtextfields
separately, i.e. can manipulate each jcomponent
, set them different values? how can achieve this?
thank in advance.
i ask possible access each jcomboboxes , jtextfields separately, i.e. can manipulate each jcomponent , set them different values? how can achieve this?
rather traversing component hierarchy (which fragile layout changes), can keep references components. following example class contains instance variables child components:
public class componentwrapper extends jcomponent{ private jcombobox combo; private jtextarea textarea; public componentwrapper(){ combo = new jcombobox(); textarea = new jtextarea(); add(combo); add(textarea); } public text gettextarea(){ return textarea; } public jcombobox getcombobox(){ return combobox; } }
the above class extends jcomponent, adds components within constructor, , can added container elsewhere. note above class example how this, , may need further adapted depending upon requirements. usage:
componentwrapper wrapper = new componentwrapper (); add(wrapper); revalidate();//if adding 'dynamically' //later, when want text string text = wrapper.gettextarea().gettext();
Comments
Post a Comment