Nesting JSplitPane Instances
The JSplitPane class manages two child components at most. However, the child components can themselves be split panes. You can create almost any combination of resizable panes by nesting split panes.
The NestedJSplitPaneTest application is shown in Listing 16.3. This application creates a horizontally aligned split pane. A separate, vertically aligned split pane is added as the right and left components in the first split pane. A label is added as the top and bottom components in each of the nested split panes. This creates a window with four regions, as shown in Figure 16.6. Moving the vertical divider alters the size of all four components displayed in the window. Moving either horizontal divider only alters the size of the two components in that nested split pane.
Listing 16.3 The NestedJSplitPaneTest Application
package com.foley.test;
import java.awt.*;
import javax.swing.*;
import com.foley.utility.ApplicationFrame;
/**
* An application that displays a JSplitPane in a frame.
* It demonstrates nested split panes.
* <p>
* @author Mike Foley
**/
public class NestedJSplitPaneTest extends Object {
/**
* The splitPane for the test.
**/
JSplitPane splitPane;
/**
* NestedJSplitPaneTest, Constructor
* <p>
* Create a splitpane that contains separate split
* panes as its children.
**/
public NestedJSplitPaneTest() {
JSplitPane right = new JSplitPane( JSplitPane.VERTICAL_SPLIT,
new JLabel( "Top Right" ),
new JLabel( "Bottom Right" ) );
JSplitPane left = new JSplitPane( JSplitPane.VERTICAL_SPLIT,
new JLabel( "Top Left" ),
new JLabel( "Bottom Left" ) );
splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
left, right );
}
/**
* @return The split pane.
**/
public JSplitPane getSplitPane() {
return( splitPane );
}
/**
* Application entry point.
* Create the frame, and display a splitpane in it.
*
* @param args Command line parameter. Not used.
**/
public static void main( String args[] ) {
JFrame frame = new ApplicationFrame( "NestedJSplitPane Test" );
NestedJSplitPaneTest test = new NestedJSplitPaneTest();
frame.getContentPane().add( test.getSplitPane(),
BorderLayout.CENTER );
frame.pack();
frame.setVisible( true );
} // main
} // NestedJSplitPaneTest
Figure 16.6 Nested JSplitPane instances.
Summary
The JSplitPane class manages two child components that can be placed side-by-side or one on top of the other. A divider visually separates the two components. The default size and look of the divider are look-and-feel specific. The user can drag the divider to alter the size of the two child components contained in the split pane. The split pane respects the minimumSize property of JComponent children. This may restrict the user from resizing the child components to arbitrary sizes.
To overcome the limitation of a split pane managing only two children, split panes can be nested. By specifying one or both children of the split pane as another instance of the JSplitPane class, you can create almost any layout.
|