• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem(s) with GridBagLayout - more confused

 
Gary Fletcher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic