Question

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);    

Edit:

    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:

java.awt.FlowLayout[hgap=5,vgap=5,align=center]

If I set the Layout in B:

    setLayout(new GridLayout(7, 2));

The information is correct:

java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]

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

Was it helpful?

Solution

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 Laying Out Components Within a Container for more details...

Update with example

Simple runnable example, using labels as place holders.

enter image description here

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

OTHER TIPS

 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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top