java - cannot add dynamic jtextfields and save their values -
in previos questions asked similar questions this. in previous projects used gui builder, add jtextfield
panel
dynamically without builder. don't why reason cannot execute code:
public class reference { jframe frame = new jframe(); jpanel mainpanel = new jpanel(); mainpanel main = new mainpanel(); jpanel subpanel = new jpanel(); jbutton addbutton = new jbutton(); jbutton savebutton = new jbutton(); private list<jtextfield> listtf = new arraylist<jtextfield>(); /** * @param args command line arguments */ public static void main(string[] args) { new reference(); } public reference() { frame.add(main); frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(addbutton, borderlayout.east); frame.add(savebutton, borderlayout.west); frame.pack(); frame.setsize(500, 300); frame.setvisible(true); main.setlayout(new borderlayout()); main.setbackground(color.green); main.add(subpanel); subpanel.setbackground(color.yellow); addbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent evt) { main.add(new subpanel()); main.revalidate(); } }); savebutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent evt) { (int = 0; < main.getcomponentcount();) { subpanel panel = (subpanel)main.getcomponent(i); jtextfield firstname = panel.getfirstname(); string text = firstname.gettext(); system.out.println( text ); }} }); } private class subpanel extends jpanel { jtextfield firstname = new jtextfield(15); public subpanel() { this.setlayout(new boxlayout(this, boxlayout.line_axis)); this.add(firstname); listtf.add(firstname); } public jtextfield getfirstname() { return firstname; } } public class mainpanel extends jpanel { list<subpanel> subpanels = new arraylist<subpanel>(); public mainpanel() { } public void addsubpanel() { subpanel panel = new subpanel(); add(panel); subpanels.add(panel); } public subpanel getsubpanel(int index) { return subpanels.get(index); } } }
and savebutton
trying value of jtextfield
, without success. in output can see jframe
2 buttons
, actionlistener
of addbutton
, savebutton
not active. cannot understand wrong.
any appreciated.
in swing, order things important, example...
frame.add(main); frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(addbutton, borderlayout.east); frame.add(savebutton, borderlayout.west); frame.pack(); frame.setsize(500, 300); frame.setvisible(true);
- you add
main
frame
- you set frames layout (!?)
- you add buttons
- you pack frame
- you set it's size (!?)
- you make visible
the problem here step #2. if, instead, remove step #2 (step #4 , #5 aren't great either), find window contains main
...
frame.add(main); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(addbutton, borderlayout.east); frame.add(savebutton, borderlayout.west); frame.pack(); frame.setvisible(true);
this...
for (int = 0; < main.getcomponentcount();) { subpanel panel = (subpanel) main.getcomponent(i);
a bad idea of 3 reasons;
- your loop never advance (
i
0
) - you blindly casting contents of
main
without knowing what's on it mainpanel
haslist
ofsubpanel
s...
you need make sure adding subpanel
s via addsubpanel
method (and should return instance of subpanel
) , provide means can access list
, maybe via getter
of sort. although, i'd more interested in values (ie text field text) rather subpanel
;)
Comments
Post a Comment