aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Some help for putting GridBagLayout to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Some help for putting GridBagLayout to work" Watch "Some help for putting GridBagLayout to work" New topic
Author

Some help for putting GridBagLayout to work

S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
Hello all, I am asking sincerely for people who have good understanding and experience of GridBagLayout to give me some help and share some thoughts here. Quite desperate.

As I have always heard that GridBagLayout is the most flexible layout manager in Swing, I have used it for quite a few projects before but after each time, I am learning more and more that the GridBagLayout has many constraints and isn't so flexible in some circumstances. There are many situations where I want components to relocate or resize itself when the user resizes the jframe

Please have a look at the attachment. I am planning to make a JFrame with those components placed on. I am not going to ask anyone to code the gui for me but I just want someone to share some thoughts with me (although I don't mind if you really write out the working code for me).


Here are some of the properties I am wishing for:
JFrame min size is 600 x 450 px, and it is resizable by user.
JLabel lbl fills the width of the content pane (sparing some space to some padding). Its height depends on the height of content
JButton btn aligns to the left of the second row. Size as normal, resizing itself to fit content
JTextArea txt is on the right of the btn, with a fixed padding in between. Width is filling up the rest of the second row. Height is fixed to a value.
space separates the second row and third row. Stretch itself to fill up the JFrame's contentPane when JFrame is being resized.
JButton hlp sits on most left of third row. Size as default
JButotn ok is on the left of JButton cnl, with a fixed padding. Spacing between hlp and ok is resizable as JFrame resizes.
JButotn cnl is on the most right of third row


OK, I assume many coders would just go straight to the code when they have scanned through the list above, as it seems very much like the code line by line. However, personally I find it difficult giving much planning.


For example, on the second row, how should I do it?
If I want to have btn's width fixed and txt taking the rest of the width, I would normally go: btn's weightx = 0 and txt's weightx = 1
And then how about setting txt's height as a fixed value? Should I use setMinimumSize/setPreferredSize? If I do that, I will have to fix the width too and we don't want that as the list specifies.


Another example, btn's width against hlp's width. As I said, the widths of btn and hlp depend on their content, so their widths may be different.
Let's say we have a case that btn's width is 80px and hlp's width is 120px. Both of these JButton have gridx = 0, so that means they are on the same column. Now if the padding space between btn and txt is 5px, the first column width, if only affected by the second row, would be 80 + 5 = 85px (let's ignore the left padding of btn for simplicity sake). Now if we look at the third row, the first column would be forced increase to at least 120 because of hlp. Because of this, the spacing of JButton btn and JTextArea txt would be forced to increase until the column width is 120. This, then, violates the list of properties above because the padding width does not match.


Third example, how to make space to stretch itself when JFrame resizes? It seems to me that the JFrame is resizing alright but its contentpane is not really resizing with it. I see all components stay where they are in the middle of JFrame.

I am really frustrated why it is so difficult/cumbersome to layout out something beautifully on Swing. Is it me that is lack of experience or is Swing's layout practice really such a chore?

Please help me. I thank you all in advanced.



[ui-design.jpg]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> people who have good understanding and experience of GridBagLayout

well, that would be (virtually no-one) very few people

> As I have always heard that GridBagLayout is the most flexible layout manager in Swing,

'flexible' is not the first description that comes to mind

from your description, and pic, some nested panels, each with a differing layoutManager,
should handle it all comfortably. You've described what is to take the extra space when resized-up,
you will also need to handle resize-down.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Look at the GridBagContraints.fill, GridBagContraints.anchor, GridBagContraints.weightx, GridBagContraints.weighy... they are very useful when working together. I have been using GridBagLayout for years and it fits all my needs from simple to complex GUI layout.


PM Me If You Want to get Urgent Help on Java Programming
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

I use GBL, but I don't usually use padding. You should make use of the columns wisely. In this case, btnHlp will be on colunm 0, ok will be in column 2 and cancel in column 3. JTextArea will start from column 1 and its width will be 3. JLabel will start at column 0 and its width will be 4. Getting the in-between space will be harder, but I have done that earlier with filler JPanels. You should give the label and textarea with fill = HORIZONTAL so that, they will take up the extra width on offer.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
Michael Dunn wrote:> people who have good understanding and experience of GridBagLayout

well, that would be (virtually no-one) very few people

> As I have always heard that GridBagLayout is the most flexible layout manager in Swing,

'flexible' is not the first description that comes to mind

from your description, and pic, some nested panels, each with a differing layoutManager,
should handle it all comfortably. You've described what is to take the extra space when resized-up,
you will also need to handle resize-down.


I guess having many sub-level layoutManager is the easiest way to achieve it... but that would probably lose the point of using GridBagLayout in the first place

Can the problem of resize-down ease off by having Min size of JFrame?
S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
Nam Ha Minh wrote:Look at the GridBagContraints.fill, GridBagContraints.anchor, GridBagContraints.weightx, GridBagContraints.weighy... they are very useful when working together. I have been using GridBagLayout for years and it fits all my needs from simple to complex GUI layout.


I appreciate your suggestions, but I am already having them all over everywhere. I just can't imagine how a pretty form can be setup nicely with GridBagLayout without using these GridBagConstraints
S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
Ranganathan Kaliyur Mannar wrote:I use GBL, but I don't usually use padding. You should make use of the columns wisely. In this case, btnHlp will be on colunm 0, ok will be in column 2 and cancel in column 3. JTextArea will start from column 1 and its width will be 3. JLabel will start at column 0 and its width will be 4. Getting the in-between space will be harder, but I have done that earlier with filler JPanels. You should give the label and textarea with fill = HORIZONTAL so that, they will take up the extra width on offer.


Good point. I have done something similar before, but I don't understand why GridBayLayout doesn't do well for the paddings and insets.... Sometimes all paddings would just disappear by random and/or stretching. I checked all the gridx and gridy, but they are all correct. The idea behind is just like positioning something or spreadsheet, I suppose? It's shouldn't be so hard to understand and picture in mind. Sometimes all components in the JFrame just flashes and moves very quickly like crazy... Don't know what happened.

Is GridBagLayout buggy?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

No, but you really have to know how to use it. It's easy to write buggy user interfaces with it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
Rob Spoor wrote:No, but you really have to know how to use it. It's easy to write buggy user interfaces with it.


I won't say I am a master of GridBagLayout, but I have read the related javadocs and understand them. I think I, to some extend, *know* how to use it
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

S Chan wrote:I won't say I am a master of GridBagLayout, but I have read the related javadocs and understand them. I think I, to some extend, *know* how to use it


well then, only way for us to help you further would be to try some code and post the same here...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some help for putting GridBagLayout to work