Chapter 16 Split Pane
- In This Chapter
- The JSplitPane Class
Many modern user interfaces allow multiple views to appear in the same frame. The user can drag the divider between the views to allocate the space given to each view. The JFC provides the JSplitPane class to manage two components that can be interactively resized by the user. Each split pane manages two components. However, the child components themselves may be instances of the JSplitPane class. The nesting of split panes allows an arbitrary number of resizable panes to be available in a single window.
In this chapter, you will learn
- How to use the JSplitPane class.
- How to configure the JSplitPane divider.
- How to nest JSplitPane instances.
The JSplitPane Class
The JSplitPane class manages two child components that are separated by a divider. When the mouse cursor is over the divider, it changes to the Resize icon. The user can drag the divider to alter the size of the two components. The components can be stacked on top of each other or be placed side-by-side.
The default JSplitPane constructor creates a split pane that positions the two managed components side-by-side. The following code fragment creates a split pane and adds two managed components:
splitPane = new JSplitPane();
splitPane.setRightComponent( rightComponent );
splitPane.setLeftComponent( leftComponent );
The setRightComponent and setLeftComponent methods specify the two components that the split pane manages. When the components are aligned top-to-bottom, the setTopComponent and setBottomComponent methods can be used to specify the managed components. If the split pane is configured right-to-left, the setTopComponent method specifies the left component and the setBottomComponent method specifies the right component. Similarly, if the split pane is configured for top-to-bottom placement, the setLeftComponent method specifies the top component and the setRightComponent method specifies the bottom component.
The current components in each region in the split pane can be queried with the getBottomComponent, getTopComponent, getRightComponent, and getLeftComponent methods. The getBottomComonent and getRightComponent methods return the same component. Similarly, getTopComponent and getLeftComponent both return the other component in the split pane. If either of the managed components is null, the other component receives the entire area allocated to the split pane, and the user cannot drag the divider. Unfortunately, there is no visual feedback to the user that the divider is disabled. The cursor also still changes to the Resize icon, telling the user that the drag operation should succeed.
The default constructor of the JSplitPane class creates two instances of the JButton class and adds them as the left and right components in the split pane. Thus, if you create a split pane using the default constructor and dont add any components, youll get a split pane containing two buttons, as shown in Figure 16.1.
Figure 16.1 The JSplitPane instance resulting from the default constructor.
The orientation for the split pane can be specified at construction or changed later with the setOrientation method. One of two constants, VERTICAL_SPLIT or HORIZONTAL_SPLIT, defined in the JSplitPane class, is passed to this method. The parameter specifies the orientation of the managed components, not the divider component. The default orientation is HORIZONTAL_SPLIT, which results in a left-to-right component placement. The current orientation can be queried with the getOrientation method. The orientation is a bound property of the JSplitPane class. The complete list of bound properties introduced in the JSplitPane class is shown in Table 16.1. The property names are constants defined in the JSplitPane class.
Table 16.1 Bound Properties Defined by the JSplitPane Class
|
Property Name
| Setter Method
| Getter Method
|
|
DIVIDER_SIZE_PROPERTY
| setDividerSize
| getDividerSize
|
ONE_TOUCH_EXPANDABLE _PROPERTY
| setOneTouchExpandable
| isOneTouchExpandable
|
LAST_DIVIDER_LOCATION _PROPERTY
| setLastDividerLocation
| getLastDividerLocation
|
ORIENTATION_PROPERTY
| setOrientation
| getOrientation
|
CONTINUOUS_LAYOUT _PROPERTY
| setContinuousLayout
| isContinuousLayout
|
|
There is a version of the constructor for the JSplitPane class in which the orientation and managed components can be specified. An example usage of this constructor is shown next. When a vertical split pane is constructed, the components are specified top and then bottom. When a horizontal split pane is constructed, the components are specified left and then right.
splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT,
topComponent, bottomComponent );
splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftComponent, rightComponent );
|