1

I have been working on creating this specific layout using GridBagLayout:

Today somehow I managed to achieve it, however I have absolutely no idea how. I understand the grid mechanics, but it is specifically the setpreferredsize override and the weightx and weighty that confuse me.

For example, changing my code's weightx from 1 to 0 will produce this:

Returning to a weightx of 1 and changing my weighty to 0 instead will produce this:

Lastly, for each of my 3 panels, I use the following code when creating it:

 JPanel connectionPanel = new JPanel() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    };

Changing the dimensions of one panel from 100 to, say 500 will produce this:

Similar happens when changing the height.

All I am trying to understand, is how the weightx, weighty, and setpreferredsize works.

When I call getpreferredsize of my panels, it returns a width and height of 0!

If I don't use the override to specifically change the preferredSize of even one of my panels, the output looks like this:

Can anyone please explain how these are related?

I've read the docs, but they are very vague. Researched for hours and still have no understanding.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KMS
  • 11
  • 3
  • `I've read the docs` - check out the section from the Swing tutorial on [How to Use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for a better explanation on how the constraints work. The tutorial also contains a working example. It is better to start with a simple example than a complex example. Also, note that when doing layout management you can nest panels with different layout managers to achieve your desired layout. – camickr Mar 05 '19 at 01:30
  • Thanks for the reply @camickr, I've read that doc but I just find the explanation to be a bit vague there for the weightx and weighty – KMS Mar 05 '19 at 01:40
  • 1) For better help sooner, [edit] to add 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 - to show how the extra space should be used. 3) `@Override public Dimension getPreferredSize()` Don't do that. The components, combined with layout padding, borders and `pack()` know better what size a component needs to be. – Andrew Thompson Mar 05 '19 at 01:46
  • Thanks for the reply @AndrewThompson 2) The first image is the goal layout, I was just confused as to how exactly I achieved that layout. I pretty much just fiddled with the weightX and setPreferredSize and somehow it all came together nicely The window will not be resizable Why is it bad to override public Dimension getPreferredSize()? I saw some other posts on the site suggested that was the way to deal with gridbaglayout Thanks – KMS Mar 05 '19 at 01:57
  • 1
    *"The first image is the goal layout, .. The window will not be resizable"* Cool. I only added that copy/paste comment in case the window size was resizable. *"Why is it bad to override public Dimension getPreferredSize()?"* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) (Yes!) IT's a valid approach for custom painted components, but even then is should be overridden, not set. .. – Andrew Thompson Mar 05 '19 at 02:04
  • .. *"other posts on the site suggested that was the way"* It's not. Note that not **every** answer here is a good one. Please link these answers so I can comment to the authors. – Andrew Thompson Mar 05 '19 at 02:05
  • 1
    `but I just find the explanation to be a bit vague` - what part do you find vague? `The window will not be resizable` - GridBagLayout allocates the space based on the preferred size of each component. (Note, each panel should calculate its own preferred size based on the components added to the panel). weightx/y only apply when the window is resizable. They indicate how to allocate extra space in the window. – camickr Mar 05 '19 at 04:30

0 Answers0