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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -