Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put two components to a JPanel with BorderLayout?

What basically i'm trying to do is to add 2 pictures, Side-By-Side in the center of a JPanel and a JLabel to the right of the JPanel, So I was told to set the layout of the JPanel as BorderLayout and to add the pictures with BorderLayout.CENTER and the JLabel with BorderLayout.EAST.

JPanel panel = new JPanel();
panel.SetLayout(new BorderLayout(100,100));

panel.add(pic1,BorderLayout.CENTER);
panel.add(pic2,BorderLayout.CENTER);
panel.add(new JLabel("Example"), BorderLayout.EAST);

actually the result is that the pictures are ON EACH OTHER TO THE LEFT of the JPanel and the JLabel is to the right of the JPanel.

Thank you very much for your help!

like image 783
Gilad Avatar asked Mar 09 '13 13:03

Gilad


2 Answers

What you need to do is have another JPanel where you add one picture to the West and one to the East. Then add that JPanel to the Center of your panel. For example

JPanel p = new JPanel(new BorderLayout());

p.add(pic1,BorderLayout.WEST);
p.add(pic2,BorderLayout.EAST);

panel.add(p,BorderLayout.CENTER);
like image 195
Kakalokia Avatar answered Nov 13 '22 05:11

Kakalokia


BorderLayout allows only one component per area. Either put them in different areas e.g. or enclose them in an additional JPanel. Or use a different LayoutManager.

like image 20
Tedil Avatar answered Nov 13 '22 04:11

Tedil