• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI - tile

 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to create the following layout using GridBag.

[button1] [button2] [button3] [button4] [button5]

[button1] [button2] [button3] [button4]

[button1] [button2] [button3]

I just cant understand how to put a button between 2..any tips?

Thanks,
Cathy.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig,

Thanks so much for the code!!
But I just realised that the post messed up my formatting:

Here is what I am trying to acheive:



Thanks,
Cathy.

[ August 07, 2005: Message edited by: Cathy Song ]
[ August 07, 2005: Message edited by: Cathy Song ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the limits of GridBagLayout is that it lays out its components in columns. This makes it difficult to layout varying numbers of components in different rows. One technique is to add components to a JPanel for some/all rows. In this case you could use a JPanel for each of the three rows and add the JButtons to the panels with FlowLayout, GridBagLayout or Box/BoxLayout. Then add the three panels to the parent container with GridBagLayout. In the code below I used a separate JPanel for the second and third rows.

Using a single instance of GridBagLayout for all three containers is okay as long as you don't need to go back and query/change any of the layouts during runtime. Using a single instance of GridBagConstraints requires care to adjust the constraints properly for each component that uses it. Some people seem more comfortable with using a new instance for each component. This is demonstrated in the 27 July 2004 tech tip article. Also, and especially in the beginning, it may seem easier to use the 'gridx' and 'gridy' constraints. I started this way and later learned to use the 'gridwidth' and 'gridheight' constraints from the api and find they are easier to work with, especially in complex layouts such as a calculator gui. GridBag seems like the mystery: we each have to find our own way.
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow Craig, thanks so much!! no wonder experimenting with the constraints was not helping me at all ...thanks so much for the very useful tip!!

-Cathy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic