Reputation: 7890
I have made JPanel
class with one JComboBox
and two JTextField
controls.
JFrame f = new JFrame();
JPanel p = new JPanel();
ComparisonPanel cp = new ComparisonPanel(); //jPanel with a 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 the ComparisonPanel
only once. I am actually trying to generate a GUI on runtime, the panel will be repeated with a few modifications (different labels etc) and then get the values from this dynamically generated GUI.
Upvotes: 2
Views: 1388
Reputation: 285430
You are setting p
's layout but not the layout of the container you're trying to add your JPanel to, the contentPane, a container that uses BorderLayout by default. BorderLayout-using containers will only all the last component added (in a default way) to be shown, and that is messing you up.
Suggestion: add your ComparsonPanel objects to the p JPanel, then add that single component to the contentPane. i.e.,
JFrame f = new JFrame();
JPanel p = new JPanel();
ComparisonPanel cp = new ComparisonPanel(); //jPanel with a 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 the layout manager tutorials.
Upvotes: 5