aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem(s) with GridBagLayout - more confused 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 "Problem(s) with GridBagLayout - more confused" Watch "Problem(s) with GridBagLayout - more confused" New topic
Author

Problem(s) with GridBagLayout - more confused

Gary Fletcher
Ranch Hand

Joined: Nov 12, 2013
Posts: 36
I am trying to use the GridBagLayout in order to develop a windowed application.

Currently I am at a VERY early stage, just trying to add 3 components to a content pane. They should simply be displayed next to each other, in the top left of the frame, but they are being displayed in the centre of the window and I do not know why. Can anyone point me in the right direction.

Code is:

Main


Frame Class


Content Pane Class

Gary Fletcher
Ranch Hand

Joined: Nov 12, 2013
Posts: 36
Also, is it possible to add a filler, or spaces, between components, or would I need to create a space JLabel and add that in the appropriate place?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
They should simply be displayed next to each other, in the top left of the frame, but they are being displayed in the centre of the window and I do not know why.


Check out the section from the Swing tutorial on How to Use Grid Bag Layout. The section on weightx / weighty will explain the problem and the solution.

The GridBagLayout is one of the most complicated layout managers to use. A simpler layout manager would be the FlowLayout. The tutorial also has examples of how to use this layout manager.
Gary Fletcher
Ranch Hand

Joined: Nov 12, 2013
Posts: 36
Thanks Rob.

Unfortuntely the FlowLayout will not be flexible enogh for the end application which requires multiple Components of various sizes, hence the dive into GridBagLayout.

I have read the tutorial and it has lead to more confusion.

The book I am reading states that values of GridBagLayout.weightx/y are between 0-100, the tutorial says between 0-1, although both could be considered a percentage weighting which is correct? Or are they both correct?
Gary Fletcher
Ranch Hand

Joined: Nov 12, 2013
Posts: 36
Stil having issues even when making the weightx/y non zero.

As the finished app will have mutilple components that need diffreent requirements I have decided to split the Componets into individual JPanels, but the initial test STILL puts the panel in the centre of the content pane, please help as I think I am going a bit mad.

Main Class


Main Frame Class


Content Pane Class


Component Class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem(s) with GridBagLayout - more confused