JPanels in JPanel


I have a problem with Java JPanels. I would like to put 2 JPanels with different layouts into one JPanel which is also has a layout. Is it even possible to make it work?

BibP() setLayout(new GridLayout(5, 1)); //The big JPanel add(new A(), new FlowLayout(4)); add(new B(), new GridLayout(7,2));

Both A and B are classes extends as JPanels and no matter what I changed or commented B is always appears in 1 row. I have 4 elements added to A and 14 to B (JLabels and JTextAreas) and there is not much code in them only the adds and some calculation.

The problem might be in the JFrame where I am trying to put the big JPanel.

JFrame.this.add(new BigP(),BorderLayout.CENTER);


public class BigP extends JPanel{ //Labels and TextAres public class A extends JPanel{ public A(){ setBorder(new EmptyBorder(0, -50, 0, 0)); //get date and add to textareas //add the label and textareas } } public class B extends JPanel{ public B (){ setBorder(new EmptyBorder(0, -50, 0, 0)); setBackground(Color.red); //colum longs for text areas less then 5 //add labels and textareas } } public BigP(){ setLayout(new GridLayout(5, 1)); setBorder(new EmptyBorder(3,-160,0,0)); add(new A(), new FlowLayout(4)); add(new B(), new GridLayout(7,2)); } }

Thanks for helping.

After a few tries:

If I used this:

add(new B(), new GridLayout(7,2));

I get this in B when I printlned the layout:


If I set the Layout in B:

setLayout(new GridLayout(7, 2));

The information is correct:


But there is only 2 JTextAreas vissible where should be 14 elements.



Is it even possible to make it work?


Yes, it's commonly know as compound layouts.

The problem you have is you are trying to supply the Layouts as constraints to the current containers layout manager (which in your case, your lucky it wasn't expecting any)...

Instead, supply the layout manager directly to the child panels...something like...

setLayout(new GridLayout(5, 1)); //The big JPanel JPanel a = new JPanel(new FlowLayout(4)); JPanel b = new JPanel(new GridLayout(7,2)); add(a); add(b);

Take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html" rel="nofollow">Laying Out Components Within a Container</a> for more details...

<strong>Update with example</strong>

Simple runnable example, using labels as place holders.

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/tnYZG.png" data-original="https://i.stack.imgur.com/tnYZG.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

import java.awt.Color; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.LineBorder; public class ExampleLayout { public static void main(String[] args) { new ExampleLayout(); } public ExampleLayout() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JPanel mainPane = new JPanel(new GridLayout(5, 1)); mainPane.add(new APanel()); mainPane.add(new BPanel()); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(mainPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class APanel extends JPanel { public APanel() { setLayout(new FlowLayout(4)); for (int index = 0; index < 4; index++) { add(new JLabel(Integer.toString(index))); } setBorder(new LineBorder(Color.RED)); } } public class BPanel extends JPanel { public BPanel() { setLayout(new GridLayout(7, 2)); for (int index = 0; index < 14; index++) { add(new JLabel(Integer.toString(index))); } setBorder(new LineBorder(Color.BLUE)); } } }

This setBorder(new EmptyBorder(3,-160,0,0)); also looks wrong. EmptyBorder should NEVER have negative values


Is it even possible to make it work?

Yes it is possible to do this. In the following line

JFrame.this.add(new BigP(),BorderLayout.CENTER);

I would suggest do

mainFrame.getContentPane.add(new BigP());

and set the layout of your mainFrame before this

mainFrame.setLayout(new GridLayout(5, 1));