Part III Container Components
- In This Part
- JPanel and Box Classes
- JTabbedPane Class
- Scrolling Components
- Split Pane
- Internal Frames
Chapter 13 JPanel and Box Classes
- In This Chapter
- The JPanel Class
- The Box Class
- The JBox Class
The simplest of the JFC container components is the JPanel class. It provides a lightweight replacement for the AWT Panel class. However, the JPanel class extends the JComponent class, not Panel as you may have expected.
The Box class is a container component that contains a BoxLayout for its layout manager. The Box class also contains methods for creating and adding invisible spacing components to the layout. These components give more options when youre laying out the children of the Box container. The Box class breaks form with the rest of the JFC containers. It does not extend the JComponent class; instead, it extends the AWT Container class. This makes Box behave differently than other JFC components. For example, the Box class does not have a border property. If a border is needed around a Box container, it must first be added to a JPanel instance, and the border must be set on the JPanel instance.
In this chapter, you will learn
- How to use the JPanel class.
- How to use the Box class.
- How to add glue, rigid, and strut components to Box.
The JPanel Class
The JPanel class is a lightweight replacement for the AWT Panel class. The primary purpose of the class is to provide a concrete container in the JFC. If you recall from the discussion of the JComponent class in Chapter 3, JComponent, it is an abstract class. As such, it cannot be instantiated. The JPanel class is provided to give a concrete container class. Being an extension of the JComponent class, it is a container and inherits the features contained in that class.
The JPanel class adds very little functionality to its inherited API. As a matter of fact, its constructors contain most of the useful functionality. The signatures of the four constructors contained in the class are shown below. As with its AWT cousin, the JPanel class is configured with a shared instance of the FlowLayout class. However, you can specify any layout manager desired in the constructor. By default, instances of the JPanel class are double buffered and opaque. As with any JComponent, these properties can be set with public methods:
JPanel()
JPanel(boolean isDoubleBuffered)
JPanel(java.awt.LayoutManager layout)
JPanel(java.awt.LayoutManager layout, boolean isDoubleBuffered)
Later in this chapter, you will see how the JPanel class can be extended to provide a custom container class.
The Box Class
The Box class is an oddity in the JFC. It provides a lightweight container that is configured with the BoxLayout layout manager. When its configured to align components along the y-axis, adding components to the Box is like stacking objects in a box (thus the name of this class and the layout manager). The BoxLayout class is a new layout manager that is part of the JFC. The Box class also contains several static methods that create invisible components to help control the layout of the container. What makes the Box class unusual is that it does not extend the JComponent class. This means that the properties contained in that class are not inherited. This includes, but is not limited to, the border property and keyboard handling. To fully understand the Box class, you must first look at the BoxLayout class.
The BoxLayout Class
The BoxLayout class is a layout manager contained in the JFC. Conceptually, it is similar to the FlowLayout layout manager contained in the AWT, in that it lays out a containers components in the order they are added to the container. However, the BoxLayout allows the components to be stacked vertically as well as placed horizontally. The BoxLayout layout manager also does not wrap components.
The orientation of the BoxLayout is specified at construction and cannot be altered thereafter. The constants X_AXIS and Y_AXIS are defined in the BoxLayout class to specify a left-to-right or top-to-bottom component arrangement, respectively. The following lines of code could be used to create a JPanel and configure it with a vertically oriented BoxLayout layout manager:
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout( panel, BoxLayout.Y_AXIS );
panel.setLayout( boxLayout );
You will notice in this code fragment that the BoxLayout class requires an AWT Container parameter in its constructor. This is unlike the AWT layout managers, which do not bind themselves to the container they are managing at construction. This reference is used internally by the BoxLayout class to ensure that instances are not shared between multiple container instances. Because the binding is an AWT Container and not a JComponent, the BoxLayout class can be used to manage any container.
The BoxLayout class respects a components minimum and maximum size properties. When the BoxLayout class is configured to stack components along the y-axis, it tries to size each component to its preferred height. The layout manager will try to size each component to the same width, the largest preferred width in the container. If the container is wider than this width, the components are sized to the containers width if possible.
Listing 13.1 contains a simple application that demonstrates this behavior. Three instances of the JPanel class are added to a panel that is configured with a vertically oriented BoxLayout layout manager. Each panel added to the outer panel is given a different background color to distinguish it from each others. The panel is shown in Figure 13.1 at its original size and then after being enlarged. As shown in the figure, the panels are resized to fill the space of the container. Also, each panel is sized equally, horizontally and vertically.
|