I've been having a problem with the GridBagLayout for a few hours but I don't get to solve it. I have two JPanels (which contain JTextFields) in a JFrame. The first (upper) JPanel has a grid of 3 rows and 2 columns. The second (lower) JPanel has a grid of 3 rows and 3 columns. I need them to be aligned as in the next figure (white column in the upper JPanel means space; the numbers indicate the relative width) and the proportions to be consistent as the size of the window changes.

The code that I have is the next:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class FrameWithTwoPanels {
private GridBagConstraints constraints;
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameWithTwoPanels window = new FrameWithTwoPanels();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FrameWithTwoPanels() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Populate the frame with two Panels
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
// PANEL 1: 3 rows and 2 columns
frame.add(this.createPanel(3, 2), c);
// PANEL 2: 3 rows and 3 columns
c.gridy = 1;
frame.add(this.createPanel(3, 3), c);
frame.pack();
frame.setLocationRelativeTo(null);
}
private JPanel createPanel(int numrows, int numcols){
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
//constraints.fill = GridBagConstraints.BOTH;
for(int row=0; row<numrows; row++){
for(int col =0; col<numcols; col++){
JTextField component = new JTextField(row + "-" + col);
if(col == 0){
constraints.weightx = 0.5;
}else if(col == 1){
constraints.weightx = 0.4;
}else if(col == 2){
constraints.weightx = 0.1;
}
constraints.gridy = row;
constraints.gridx = col;
panel.add(component, constraints);
if(numcols == 2 && col == 1){
// Add auxiliar panel
constraints.weightx = 0.1;
constraints.gridx = col + 1;
panel.add(new JLabel());
}
}
}
panel.setBorder(new TitledBorder("Number of columns: " + numcols));
return panel;
}
}
The code gives the form shown in the screenshot:

This is a circumstance where you're actually better off using plain GridLayout. I think the following code does what you want:
private JPanel createPanel(int numrows, int numcols) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(numrows, 3));
for (int row = 0; row < numrows; row++) {
for (int col = 0; col < numcols; col++) {
JTextField component = new JTextField(row + "-" + col);
panel.add(component);
if (numcols == 2 && col == 1) {
JPanel spacer = new JPanel();
panel.add(spacer);
}
}
}
panel.setBorder(new TitledBorder("Number of columns: " + numcols));
return panel;
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With