wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes GUI - tile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI - tile" Watch "GUI - tile" New topic
Author

GUI - tile

Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
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

Joined: Jan 14, 2004
Posts: 1535
Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Jul 01, 2003
Posts: 92
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI - tile
 
Similar Threads
Layout problems!!
change font in JPanel
which layouts to chose ( begging stuff )?
requestFocus
how to run sample swing program