We would like to know how to prevent GridBagLayout from resizing columns.
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; /* w w w . ja va 2 s.c om*/ import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Main { static int value = 99; public static void main(String[] args) { JFrame frame = new JFrame(); final JLabel valueLabel = new JLabel(String.valueOf(value)); JButton decButton = new JButton("-"); decButton .addActionListener(e -> valueLabel.setText(String.valueOf(--value))); JButton incButton = new JButton("+"); incButton .addActionListener(e -> valueLabel.setText(String.valueOf(++value))); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1; c.gridx = 0; c.gridy = 0; panel.add(decButton, c); c.gridx = 1; panel.add(valueLabel, c); c.gridx = 2; panel.add(incButton, c); c.gridy = 1; int w = 32; for (c.gridx = 0; c.gridx < 3; c.gridx++) { panel.add(Box.createHorizontalStrut(w), c); } frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); } }