I have a GUI with four components laid out next to each other with GridBagLayout. When I resize the window to the right, the leftmost component shakes, when I resize to the left, the rightmost.
Here is an example:
import javax.swing.*;
import java.awt.*;
public class GBLShakeTest extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("GBLShakeTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));
frame.add(new GBLShakeTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public GBLShakeTest() {
super(new GridBagLayout());
add(new JTextArea("component 1"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
add(new JTextArea("component 2"), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
add(new JTextArea("component 3"), new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
add(new JTextArea("component 4"), new GridBagConstraints(3, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
}
}
It even happens with the GridBagLayout demo from oracle which uses only JButtons. Resizing to the right is fine, but when you enlarge the window leftwards, the right Button shakes. Does anyone know how to fix this?