This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
[ August 07, 2005: Message edited by: Cathy Song ] [ August 07, 2005: Message edited by: Cathy Song ]
Joined: Jan 14, 2004
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.
Joined: Jul 01, 2003
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!!