java - How to add a custom jPanel to jFrame multiple times? -
i have made jpanel
class 1 jcombobox
, 2 jtextfield
controls.
jframe f = new jframe(); jpanel p = new jpanel(); comparisonpanel cp = new comparisonpanel(); //jpanel few elements comparisonpanel cp2 = new comparisonpanel(); p.setlayout(new flowlayout()/*new gridlayout(2, 2)*/); f.getcontentpane().add(cp); f.getcontentpane().add(cp2/*cp*/); f.setvisible(true);
it shows comparisonpanel
once. trying generate gui on runtime, panel repeated few modifications (different labels etc) , values dynamically generated gui.
you setting p
's layout not layout of container you're trying add jpanel to, contentpane, container uses borderlayout default. borderlayout-using containers last component added (in default way) shown, , messing up.
suggestion: add comparsonpanel objects p jpanel, add that single component contentpane. i.e.,
jframe f = new jframe(); jpanel p = new jpanel(); comparisonpanel cp = new comparisonpanel(); //jpanel few elements comparisonpanel cp2 = new comparisonpanel(); p.setlayout(new flowlayout()/*new gridlayout(2, 2)*/); p.add(cp); p. add(cp2); f.getcontentpane().add(p); f.pack(); f.setvisible(true);
better yet, read layout manager tutorials.
Comments
Post a Comment