Swing Components / The Box Layout Manager
Subtitles of the Movie
You can use any of the number of layout managers to position the components on a window. Some are very simple and they make all the decisions about positioning things for you. The ones that give you more control over the layout are a bit more complicated to use. A good compromise and one I really like is the BoxLayout Manager. You can use it to build most any window you like. Here is a very simple example. This layout is simply a horizontal row of push buttons. As you can see, this class is a JFrame. That means it's the main window of a program. The actual layout of the buttons is done in the method build frame. First a call is to made to get content pane to return the container of the JFrame window. A BoxLayout manager is constructed and stored in the container so it will become the layout manager. The layout manager is now installed in the window and waits for you to add components to its list of things to be managed. Notice that this BoxLayout Manager was constructed to be a horizontal box. That way each component that's added to it will be inserted in a row from left to right. Here five buttons are constructed and added. Each one is inserted in the container by the add method. Up until here, the buttons have only been added to the layout manager, but no action has been taken. The pac method is called to instruct the layout manager to do this job of positioning everything in the list. Back to where the method was called, after the call to build frame, the location of where the window is to appear is set. If you don't set this location, it appears only in the upper left-hand corner. A call is to made to set visible, which immediately makes the window appear and when you run it, there are all the buttons in a horizontal row across the window. The box expands itself vertically to fit the height of the buttons and it expands itself horizontally to total width of all the buttons. Now, you can also put buttons in a vertical box. This is exactly the same program except for one thing. The BoxLayout manager was constructed to align the components along the Y axis. So when you run it, the buttons come out like this, but the buttons are different widths and flush against one side. Now, some things can be done to fix all that. Here is an aligned version of the same program. A couple of method calls were added for each button. This is the addition of a rigid area component. It takes up space in the window but doesn't actually display anything. You add rigid areas to the box just like you would add any other component. The result is they add space between the buttons and between the tops and bottoms and edges of the window. Also notice that the rigid area is a hundred pixels wide. That's wider than the buttons, so the frame will be wider than the buttons and the margins on the edges. Also notice the horizontal alignment of the buttons is set to the center of the window. The result looks like this. Alright, as you can now see, the buttons are all centered in the window and they have a margin around them, but the buttons all vary in size. Here is one more version of the program with the buttons all set to the same size. Now, every component has three sizes; a minimum, a maximum and a preferred size. This method is called for all the buttons. So effectively it makes the buttons all inflexible. As a result, the display now looks like this. The buttons are now all the same size as in evenly spaced. But we can do a lot more with the BoxLayout than just put components in a row. Let me show you a simple example. This is an example of boxes within boxes. The main window layout has a vertical box with a label at the top, a horizontal box with a couple of text areas in the middle and another horizontal box with a row of buttons at the bottom. There are a couple of new things here I need to show you. Now, boxes always change their shape and size to hold whatever you decide to put in them. To nest one BoxLayout manager inside another, it's necessary to put a JPanel in the existing box and then assign a new BoxLayout manager to it. The components can be added to the new layout manger. Here you can see that two text areas have been added, separated by rigid areas. The same sort of thing was done to add the third box, the button box at the very bottom. Here you can see where a JPanel was added to the bottom box and a horizontal BoxLayout manager assigned to it. The first thing that was added to the horizontal box was a horizontal glue component. Now, glue is not a very good name for what that thing does. What it should be called is something like put any extra space that you want to have here. If you were to put in two or more glue components, they would equally share the leftover space. In this case, the glue item is added before the buttons so the buttons are all shifted over to the right. When you use boxes to build a window, the construction is fairly easy. You can't necessarily shape it exactly the way you want, but you can get it very close. Now, coming up I'm going to show you how to place components exactly where you want them.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 