1

I have following code:

public Frame() {
    super();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridLayout(0, 3));
    for (int i = 0; i < 30; i++) {
        add(new JLabel("label"));
    }

}

All this works perfectly fine, untill you start vertically resizing the frame.
For some reason it creates a gap above and under the labels that grows and shrinks again when enlarging the frame.

This is most notable by looking at the top-line of labels that keep going up and down when you pull the underside of the frame further down.

For clarifcation, these are the problem areas.
The two areas in red are also filled while i want them to be empty.

Any idea how to get rid of this empty space?
Thanks

dashhund
  • 322
  • 3
  • 17

1 Answers1

2

For some reason it creates a gap above and under the labels that grows and shrinks again when enlarging the frame.

When you pack the frame the components are displayed at their preferred size when using a GridLayout.

However, when you increase the size of the frame, a GridLayout tries to fill all the space available. This means that all the components will grow.

I'm not sure if this is the same when horizontally resizing, at the very least it isn't that obvious.

It is not obvious because you only have a single column of labels. Change the GridLayout to have multiple columns to see what happens.

how to fix this?

Use a different layout manager. Maybe a BoxLayout or GridBagLayout. See the Swing tutorial on Layout Managers for more information and examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I was reading through this, thinking *"He's going to mention [Relative Layout](http://tips4java.wordpress.com/2008/11/02/relative-layout/) (as also seen [here](http://stackoverflow.com/a/21145798/418556)) any moment now!"*. You don't think that it is the same basic problem/effect with `GridLayout` that was identified in that Q&A? – Andrew Thompson Mar 24 '14 at 03:01
  • @AndrewThompson, no I don't think this is the same. I think the OP is just trying to display the labels vertically at their preferred size and does not want them to dynamically resize, which is why I suggested two other layout managers that can be used for this requirement. In your example you wanted dynamic resizing, but the GridLayout did not allow you do control where the extra pixels caused by rounding were allocated. – camickr Mar 24 '14 at 04:58
  • Thanks for clarifying. I had noticed the OP commented *"For some reason it creates a gap above and under the labels that **grows and shrinks again**"* and it made me think it might be a similar problem. Of course, an [MCVE](http://stackoverflow.com/help/mcve) or [SSCCE](http://sscce.org/) (or [screenshots](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post)) would be helpful here, so we can actually ***see*** the effect. ;) – Andrew Thompson Mar 24 '14 at 05:16
  • I DO have the intention of making my GUI dynamically resizable and have already tried BoxLayout and GridBagLayout, I just used GridLayout to have a simple example. I also added a screenshot (hyperlink due to lack of rep) for clarification ;) – dashhund Mar 24 '14 at 10:55
  • @dashhund, if you are saying you want empty space before/after the first/last label then add "glue" at the beginning/end of the panel. The BoxLayout tutorial explains what glue is. – camickr Mar 24 '14 at 16:49
  • @camickr No, i need the opposite, i want there to be no empty space at all :p – dashhund Mar 24 '14 at 17:23
  • @dashhund, `i want there to be no empty space at all`, then what is the problem? I gave you the solution with my original answer. – camickr Mar 24 '14 at 17:26
  • @camickr Let me rephrase that, at the marked zones on the picture (link provided) i want there to be no empty space at all, the rest of the empty spaces is intended, so a simple switch of layout won't do the job – dashhund Mar 24 '14 at 20:48
  • @dashhund, yes a switch of layout will work. Both the BoxLayout and GridbagLayout allow you to add space between components. You can do whatever you want. Read the tutorial!!! – camickr Mar 24 '14 at 21:05
  • I have, but i believe you still don't fully understand what i want. Like i said before, i DO want a resizable gui with variable spaces in between, just not before the first and after the last element. So going with the insets isn't really an option. – dashhund Mar 24 '14 at 22:04
  • @dashhund, Did you read my suggestion? I never said you needed to use "insets". However I do remember mentioning "glue". – camickr Mar 25 '14 at 01:29