|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Becoming Friends with GridBagLayout
Becoming Friends with GridBagLayout
By: John Tabbone
Apr. 1, 1998 12:00 AM
My last column focused on several of Java's LayoutManagers, which are constructs used by developers to position Components within Containers using logic instead of pixel coordinates. We discussed all of Java's LayoutManagers except GridBagLayout, which is the focus of this article.
Of all of Java's LayoutManagers, GridBagLayout offers the developer the most precision in positioning Components. This ability comes at a great expense, as GridBagLayout is the most complicated (and probably the most poorly documented) LayoutManager in the AWT. GridBagLayout works integrally with a helper class called GridBagConstraints to place Components on the screen. Basically, a developer will set values in the GridBagConstraints object that specify things such as: A method provided in GridBagLayout will bind the constraints to a particular Component. When the Component is added to the Container (using the add method), GridBagLayout will use the information in the Components corresponding GridBagConstraints object to determine the Component's position and size.
In short, the process is: Most of the work involved in using GridBagLayout is setting the constraints correctly.
As the name implies, GridBagLayout has something to do with a grid (and a bag). GridBagLayout is similar to GridLayout in the following regards:
The differences between GridLayout and GridBagLayout are more striking: Also, GridBagLayout offers no methods to explicitly define the number of rows or columns in the grid, nor does it have methods to define the size of each cell in the grid. Instead, GridBagLayout calculates the number of rows and columns in a grid by the number of Components placed on the screen. If a container has five Components lined up horizontally, then the grid consists of five columns and one row. If the Container has five Components lined up vertically, then the grid consists of one column and five rows. So how do you know how many columns and rows your GUI will have? Well, the best way is to hand draw your gui and create the grid for yourself. Figure 1 shows a standard source/destination kind of gui. Note the dashed lines and numbers. We have gone along the X axis and made a mark when we came into contact with the upper-left hand corner of a Component. We have done the same thing along the Y axis. Keep in mind when you do this on your own that you should mark only the upper left hand corner of the Component. After the exercise is complete, you are left with the grid your GridBagLayout will use to place Components. Once the grid is complete, we have some very important pieces of information; the upper left hand coordinate of each Cell, and the width and height of each Cell. GridBagConstraints has data members that maintain this information and need to be set for the gui to work. The data members are: gridx, gridy, gridwidth and gridheight. Look at Figure 2 to see the cells of the grid and Table 1 to see the GridBagConstraints attributes for each Component. In our program, we have created a helper method, addComponent, to assist in setting the constraints. Notice that we do not have to create a new GridBagConstraints object every time we add a Component. Also notice the sequence of events: First we set constraints, then we bind the constraints to the Component using the setConstraints method and finally we add the Component. Be sure to do things in this order to avoid confusion! Look at Listing 1 for the complete code listing. And congratulations! If you can get this far, you have done the hard part. Once the program has been compiled and executed we find that the results are not quite the same as our intention. The code needs some refinement. Look at Figure 3 for the output of the program. Notice that the four buttons in the center are not aligned, and everything is squished together in the center of the screen. The remaining tasks involve setting more constraints attributes on each Component.
GridBagConstraints.fill
GridBagConstraints.anchor
GridBagConstraints.weightx and GridBagConstraints.weighty Note also that weights affect the cell size, not the Component size. Here is where some of the other attributes of GridBagConstraints will play an important role. If the fill is set to something other than NONE, and a cell has a non-zero weight, the Component itself will grow to fill any newly acquired space in the cell. Alternatively, if an anchor is set to something other than CENTER, and a cell has a weight other than zero, the positioning of a Component within a cell will be more apparent. Play around with it!
GridBagConstraints.insets
GridBagLayout Tips I have not discussed GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER. Don't use them until you read my next column! As with any other topic in programming, the best way to learn it is to play around with the code, so please do so. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||