0

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.

Demo

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;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RHo
  • 3
  • 3
  • Nope, this is pretty much expected behaviour, as components which are invisible are not used to calculate the layout requirements. Maybe use a blank image or component instead – MadProgrammer Mar 21 '16 at 08:36
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. (Also apply that logic to if components are toggled for their visibility.) – Andrew Thompson Mar 21 '16 at 08:43

3 Answers3

1

One option you have is that, you can envelope the buttton with a JPanel that has constant size, then if you hide button, the JPanel will still be there to hold the space.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • 1
    *"a JPanel that has constant size"* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) OTOH using a `CardLayout` on that panel might have the required effect. A container with a card layout will be as wide as the widest component/container added to it, and as tall as the tallest - no matter which card is visible. [This answer](http://stackoverflow.com/a/5786005/418556) contains a simple demo. of card layout. – Andrew Thompson Mar 21 '16 at 08:46
  • You could use ipadx and ipady constraints instead of changing the size of the container itself – MadProgrammer Mar 21 '16 at 09:43
0

I solved this problem with an empty png that is 25x25 pixel that is at the same position as the button that gets shown and hidden.

Will updated this in the future with a better solution.

Edit

I used the DesigneGridLayout to solve this problem.

JPanel panel = DefaultLayouts.panel();
DesignGridLayout layout = new DesignGridLayout(panel);

JTextField orderNumberField = new JTextField();
JButton searchButton = DefaultLayouts.imageButton("SEARCH");
JButton checkButton = DefaultLayouts.imageButton("CHECK");

layout.row().grid().add(new JLabel("Auftrag Nummer:")).add(orderNumberField);
layout.emptyRow();
layout.row().grid().add(searchButton).add(checkButton);
RHo
  • 3
  • 3
0

In addition to setting the components whose sizes you would like to keep static on a JPanel with a specified dimension, If the layout is the dominant one on the frame, try using frame.setResizable(false);.

Nkem
  • 17
  • 6