I have GridBagLayout where I add a JLabel
, a JTextfield
and two JButtons
with and image, the image is 25x25 pixel. Whenever I show or hide the button the whole layout moves. I tried to setSize()
, setMinimumSize()
, setPreferredSize()
and setMaximumSize()
but it will just ignore that an resize the JLabel
and the JTextField
. How can I set a fix size for these two components or just make all the cells in the GridBagLayout the same size.
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("Auftrag Nummer:"), c);
JTextField orderNumberField = new JTextField();
c.gridx = 1;
c.gridy = 0;
panel.add(orderNumberField, c);
JButton searchButton = DefaultLayouts.imageButton("SEARCH");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10, 0, 0, 0);
panel.add(searchButton, c);
JButton checkButton = DefaultLayouts.imageButton("CHECK");
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(10, 0, 0, 0);
panel.add(checkButton, c);
DefautlLayouts.imageButton
gets the path and the tooltip and sets some defaults
public static JButton imageButton(String propertiesName) {
JButton button = DefaultLayouts.button();
button.setIcon(new ImageIcon(imagesProperties.getProperty(propertiesName + "_PATH")));
button.setToolTipText(imagesProperties.getProperty(propertiesName + "_TOOLTIP"));
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
return button;
}