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 Layout
s 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.
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