Debugging GridBagLayout - components clumped to center - java

I'm trying to get the following layout
Label..................NumericField
Label2................NumericField
Label3333..........NumericField
Basically the (.) dots would be empty space. I had tried GridBagLayout with making the label's gridwidth as 5 and the NumericField's gridwidth as 1. I'm posting the code below. But I don't see the desired result and I see all components aligned at the center instead of Labels being at left border and NFs being at right border.
For Labels:
GridBagConstraints localC = new GridBagConstraints();
localC.anchor = GridBagConstraints.FIRST_LINE_START;
//localC.fill = GridBagConstraints.HORIZONTAL;
localC.weightx = 1.0;
localC.weighty = 1.0;
localC.gridx = 0;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 5;
localC.insets = new Insets(0, 0, 0, 0);
For NumericFields
localC.anchor = GridBagConstraints.RELATIVE;
localC.weightx = 0.5;
localC.weighty = 0.5;
localC.gridx = 1;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 1;
I'm new to JAVA and struggling with layouts generally.

Add a value to the Insets right property, which will add that number of pixels to the right side of the column. You could also use GridBagConstraints#anchor set to GridBagConstraints.WEST, which will force the components in the columns to be positioned on the left hand side of the "column", this ensures that when a component in the column is wider, they won't be laid out in the middle of the resulting space.
gridwidth determines how a given cell will span across multiple columns, but if there are no other components in the resulting columns, they are discard (defaulted to 0), so in your layout, it's meaningless.
See How to Use GridBagLayout more details
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 0, 0, 12);
add(new JLabel("Label"), gbc);
gbc.gridy++;
add(new JLabel("Label2"), gbc);
gbc.gridy++;
add(new JLabel("Label3333"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(0, 0, 0, 0);
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
}
}
}

Related

Asymmetrical Layout in Java Swing

Is it possible to create asymmetrical layouts (as opposed to square layouts) in Java Swing ? I'm thinking of something like this:
Is this achievable with any layout manager ? If not, I'd be interested in an explanation, too.
The short answer is, yes, the long answer is some what more complicated.
To start with, I'd be using GridBagLayout as my primary choice of layout manager, but I might consider GridLayout and BorderLayout as additional options. The point is, you want to break down your layout into manageable chunks of functionality and figure out the best solution to solve it's particular problems. You then want to piece together these individual elements back into a large picture, using the layout managers most appropriate to solve the problem each piece presents.
I'd just like to say that your basic layout screams JTable to me.
If, however, you're interested in a non-rectangular window, then it becomes somewhat more difficult, mostly because Java doesn't support decorated transparent windows (ie, windows with native frames)
If not, I'd be interested in an explanation, too
Okay, this is some what more complicated, but, it basically comes down to the fact that everything painted on the screen is contained in a, rectangular bounding box. This box was filled with a background color and the content was painted onto it.
This was done for efficiency, because everything behind this bounding box doesn't need to be painted.
As hardware became faster and rendering pipelines took more advantage of higher end libraries, like DirectX and OpenGL, it became possible to start dealing with opacity across a broader spectrum of the system, such as individual windows.
So, even when you see that really cool, curvy, funky looking UI, it's contained within a rectangular bounding box, which is transparent :/
This is pretty basic graphics concepts. Remember, it's much easier and faster to calculate a rectangular bound box (intersections/hit detection/etc) then a non-rectangular one
Per-pixel alpha is actually rather intensive to perform, which is another reason it wasn't original used at a OS/every day level, the system resources could be better used for other things
Runnable Example
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTH;
add(new FieldsPane(), gbc);
gbc.gridx++;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(new JTextArea(20, 20)), gbc);
}
}
public class FieldsPane extends JPanel {
private JPanel fields;
private JLabel filler;
public FieldsPane() {
setBorder(new LineBorder(Color.GRAY));
fields = new JPanel(new GridBagLayout());
filler = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
fields.add(filler, gbc);
addFields(new JLabel("Col1"), new JLabel("Col2"), new JLabel("Col3 "));
addFields(new JTextField(10), new JTextField(10), new JTextField(10));
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(fields, gbc);
JPanel buttons = new JPanel(new GridBagLayout());
JButton add = new JButton("Add");
JButton remove = new JButton("Remove");
buttons.add(add);
buttons.add(remove);
gbc.gridy++;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(buttons, gbc);
}
protected void addFields(JComponent col1, JComponent col2, JComponent col3) {
GridBagLayout layout = (GridBagLayout) fields.getLayout();
GridBagConstraints gbc = layout.getConstraints(filler);
fields.add(makeRow(col1, col2, col3), gbc);
gbc.gridy++;
layout.setConstraints(filler, gbc);
}
protected JPanel makeRow(JComponent col1, JComponent col2, JComponent col3) {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.weightx = 0.33;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(col1, gbc);
panel.add(col2, gbc);
panel.add(col3, gbc);
return panel;
}
}
}

GridBagLayout Alignment Issue with Button expanding Column Width

I am trying to create an interface with some JLabels icons ( box icons )and an exit button. The thing is that, when I place the button under the labels, they split, according to the button's position like this:
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
add(new JLabel(" "), c);
for(int i = 0; i < label.length; i++){ // Adding the JLabels
c.fill = GridBagConstraints.NONE;
c.weighty = 0;
c.gridy = 1;
c.gridx++;
add(label[i], c);
}
// Adding the button
c.gridy ++;
c.gridx = 2; // Changing the value of gridx, will move the button to that location and it will split another jlabel.
c.weighty = 0.09;
add(eButton, c);
revalidate();
repaint();
GridBagLayout works a lot like a grid paper, it has rows and columns, components are placed within those cells; you can specify how additional space is distributed and components are aligned within those cells.
Each row and column is sized based on the layout requirements of the other components in that row/column, this means that a component may have more space than it needs, because some other component in the same row/column needs more space to be laid out.
You can also allow a component to span across multiple rows/columns, allowing to occupy more space.
So, basically, what you need to do is tell the button that it can occupy all the columns for it's row (or at least the 5 columns which is occupied by the labels). You will need to then tell the button that it needs to be aligned to the center of it's area.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
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 Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
for (int i = 0; i < 5; i++) { // Adding the JLabels
c.fill = GridBagConstraints.NONE;
c.weighty = 0;
c.gridy = 1;
c.gridx++;
c.insets = new Insets(4, 4, 4, 4);
JLabel label = new JLabel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
};
label.setBorder(new LineBorder(Color.DARK_GRAY));
add(label, c);
}
c.gridy++;
c.gridx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.NORTH;
// c.weighty = 0.09;
add(new JButton("Exit"), c);
}
}
}
Take a closer look at How to Use GridBagLayout for more details

How to make a Java GridBagLayout with six rows and 2 columns

I'm tired with Java GridBagLayout. I want to create a panel that looks like the one shown in the picture below, but I couldn't get it. I'm unable to position the left panel, and I failed to make panels long. The width does not increase when I set gridWidth. I can't use any GUI builders for that. I want to get a layout like the picture below.
This is the unsuccessful code:
public class gui3 extends Frame{
Panel p1,p2,p3,p4,p5,p6,p7,pmain;
gui3(){
setVisible(true);
setSize(500,500);
setTitle(" Calculator ");
GridBagLayout gb1=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
setLayout(gb1);
p1=new Panel();
p1.setBackground(Color.BLACK);
gbc.gridx=5;
gbc.gridy=0;
gbc.gridwidth=3;
//gbc.weightx =0.5;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p1,gbc);
p2=new Panel();
p2.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p2,gbc);
p3=new Panel();
p3.setBackground(Color.GREEN);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p3,gbc);
p4=new Panel();
p4.setBackground(Color.cyan);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p4,gbc);
p5=new Panel();
p5.setBackground(Color.RED);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=2;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p5,gbc);
p6=new Panel();
p6.setBackground(Color.pink);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=2;
// gbc.weightx = 1;
// gbc.fill=GridBagConstraints.HORIZONTAL;
add(p6,gbc);
p7=new Panel();
p7.setBackground(Color.yellow);
gbc.gridx=6;
gbc.gridy=0;
gbc.gridheight=6;
// gbc.weightx = 1;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(p7,gbc);
}
}
You mean something like...
So basically, you can use GridBagConstraints#gridheight (or gridwidth) to set the number of grid cells that a component will span across
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.weightx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(createPane(Color.RED), gbc);
gbc.gridy++;
frame.add(createPane(Color.GREEN), gbc);
gbc.gridy++;
frame.add(createPane(Color.BLUE), gbc);
gbc.gridy++;
frame.add(createPane(Color.CYAN), gbc);
gbc.gridy++;
frame.add(createPane(Color.MAGENTA), gbc);
gbc.gridy++;
frame.add(createPane(Color.ORANGE), gbc);
gbc.gridy++;
frame.add(createPane(Color.PINK), gbc);
gbc.gridx++;
gbc.weightx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.gridheight = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.VERTICAL;
frame.add(createPane(Color.YELLOW), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
}
Have a look at How to Use GridBagLayout for more details
The abrupt changes in size from the MadProgrammer's solution can be
easily fixed in two ways:
public JPanel createPane(Color color) {
JPanel pane = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
#Override
public Dimension getMinimumSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(color);
return pane;
}
We also provide a mimimum size which is equal to the preferred size.
This way the panels are not shrinking but they are cut from the window
when the container is too small to display them.
The optimal solution is probably to set the weighty to 1. The panels will
gradually being shrinking.
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
Finally, I also created a solution with MigLayout. If possible, try
to create your layouts with MigLayout rather than with GridBagLayout.
package com.zetcode;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPanels extends JFrame {
public MigLayoutPanels() {
initUI();
setTitle("Panels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("wrap"));
add(createPanel(), "w 200, push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "push, grow");
add(createPanel(), "cell 1 0 1 6, growy");
pack();
}
public JPanel createPanel() {
JPanel pnl = new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pnl.setBorder(BorderFactory.createEtchedBorder());
return pnl;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutPanels ex = new MigLayoutPanels();
ex.setVisible(true);
}
});
}
}
GridBagLayout is a powerful manager which can be used to create most layouts.
Many programmers find it difficult to use. MigLayout is easier to understand,
more powerful, and much less verbose.

How can I align the JLabel to the left of the panel

I want to align all the JLabels to the left side of the panel. Following is my code, but it doesnt work properly, I dont know why.
JFrame frame1 = new JFrame("Register a passenger");
frame1.setVisible(true);
frame1.setSize(550, 200);
JPanel panel = new JPanel();
frame1.add(panel);
JLabel label1 = new JLabel("Name",SwingConstants.LEFT);
JLabel label2 = new JLabel("Activities",SwingConstants.LEFT);
JButton jbtReg = new JButton("Register");
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(jbtReg);
Based on your example, you could use
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
But this will align all your components to the left.
You could also consider using a different layout manager or combination of layout managers?
Take a look at A Visual Guide to Layout Managers for more ideas
Updated
FlowLayout (which is the default layout manager for JPanel) doesn't give you a lot of options, instead consider trying to use a different layout manager or combination of layout managers, for example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutExample {
public static void main(String[] args) {
new LayoutExample();
}
public LayoutExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Name:"), gbc);
gbc.gridy++;
add(new JLabel("Activity:"), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(20), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
add(new JButton("Register"), gbc);
}
}
}

Making a JPanel and inside add multiple JPanels

I need to build in swing a screen that is something like this image. I have a main panel and in that panel I need to add multiple columns 2 vertical columns and 3 horizontal. This columns are Jpanels. I tried to use GridLayout but I did not succeeded.
Very rarly will a single layout manager do everything you want. You want to start using compound layout managers where you can/need.
This example uses both a GridLayout and a GridBagLayout
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class BadLayout21 {
public static void main(String[] args) {
new BadLayout21();
}
public BadLayout21() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new GridBagLayout());
JPanel leftPane = new JPanel(new GridLayout(2, 0, 0, 4));
leftPane.add(createPane(Color.RED));
leftPane.add(createPane(Color.RED));
JPanel leftMiddlePanel = createPane(Color.BLUE);
JPanel rightMiddlePanel = createPane(Color.BLUE);
JPanel rightPane = new JPanel(new GridLayout(2, 0, 0, 4));
rightPane.add(createPane(Color.GREEN));
rightPane.add(createPane(Color.GREEN));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.insets = new Insets(20, 20, 0, 0);
add(leftPane, gbc);
gbc.gridx = 3;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(20, 0, 0, 20);
add(rightPane, gbc);
gbc.weightx = 0;
gbc.gridx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(0, 0, 0, 10);
add(leftMiddlePanel, gbc);
gbc.gridx = 2;
gbc.insets = new Insets(0, 10, 0, 0);
add(rightMiddlePanel, gbc);
}
protected JPanel createPane(Color color) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(25, 25));
panel.setBackground(color);
return panel;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I think it will be difficult to place all the panels directly in one big panel. Multiple solutions are possible. Like dividing the the main panel in four seperate (vertical) panels and add the final panels to those four sub-panels. Or as freak suggested, start with a border layout, and first create a left-subpanel, center-subpanel and right-subpanel, each with their own layout managers. And in those sub-panels you can place your final panels.
Although the precise purpose or goal is a little fuzzy for me right now, I hope you can use my suggesten.

Categories