I l@ve RuBoard |
![]() ![]() |
11.1 The JSplitPane ClassThe JSplitPane component allows you to place two (and only two) components side by side in a single pane. You can separate the pane horizontally or vertically, and the user can adjust this separator graphically at runtime. You have probably seen such a split pane approach in things like a file chooser or a news reader. The top or left side holds the list of directories or news subject lines while the bottom (or right side) contains the files or body of the currently selected directory or article. To get started, Figure 11-2 shows a simple split pane example that shows two text areas with a horizontal split. You can adjust the width of the split by grabbing the divider and sliding it left or right. Figure 11-2. Simple JSplitPane with two text areas![]() Even with the code required to make the text areas behave (more on that in Chapter 19), the following example is still fairly simple. If you are looking to get up and running with a quick split pane, this is the way to go. // SimpleSplitPane.java // A quick test of the JSplitPane class // import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleSplitPane extends JFrame { static String sometext = "This is a simple text string that is long enough " + "to wrap over a few lines in the simple demo we're about to build. We'll " + "put two text areas side by side in a split pane."; public SimpleSplitPane( ) { super("Simple SplitPane Frame"); setSize(450, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); JTextArea jt1 = new JTextArea(sometext); JTextArea jt2 = new JTextArea(sometext); // Make sure our text boxes do line wrapping and have reasonable minimum sizes. jt1.setLineWrap(true); jt2.setLineWrap(true); jt1.setMinimumSize(new Dimension(150, 150)); jt2.setMinimumSize(new Dimension(150, 150)); jt1.setPreferredSize(new Dimension(250, 200)); JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jt1, jt2); getContentPane( ).add(sp, BorderLayout.CENTER); } public static void main(String args[]) { SimpleSplitPane ssb = new SimpleSplitPane( ); ssb.setVisible(true); } } 11.1.1 PropertiesTable 11-1 shows the properties contained in the JSplitPane class.
The properties of JSplitPane primarily relate to the divider. You can get and set its size, location, and orientation, and its minimum and maximum bounds. Of particular interest is the oneTouchExpandable property. If this value is set to true, the UI should provide a component that can quickly collapse or expand the divider. For your programming convenience, four component properties are available. Note that the component properties bottomComponent and rightComponent refer to the same object, as do topComponent and leftComponent. This way, you can refer to your components in a fashion that's consistent with the orientation of your split pane. The resizeWeight property affects how new space is allocated if the split pane itself is resized. The default of 0.0 distributes all of the size change to the bottom-right component; 1.0 goes entirely to the top-left component. If the continuousLayout property is true, both sides of the pane are updated as often as possible while the user moves the divider. Otherwise, the components are resized and redrawn only after the divider location is set. Continuous layout can be a performance problem and is often just awkward. The lastDividerLocation property saves the previous location of the divider and can be used to undo a change in the divider's position. 11.1.2 ConstantsSeveral constants are defined for use with the JSplitPane class (see Table 11-2). Some of these constants name the various properties in a split pane while others provide constraints for where to place a component or where to place the split.
11.1.3 Constructors
11.1.4 Control Methods
11.1.5 Minimum and Preferred SizesWhen setting up your split panes, watch out for the minimum and preferred sizes of the two components. If you look back at the code for Figure 11-2, you can see we forcibly set the minimum sizes of the two text areas. The boundaries observed by the divider in a split pane are dictated by the minimum sizes of the two components. Some components, such as JTextArea, define their minimum size as the size they are initially shown with. That often works fine, but in the case of the split pane, it means that you cannot adjust the division between the two text areas (as both are already at their minimum sizes). The same is true for containers such as panels or the JScrollPane we discuss in the next section. You should also set the preferred size of the first component if you want the split pane to come up correctly the first time. In the previous example, if you remove the line that sets the preferred size of jt1, then jt1 comes up with room for one row of text, and jt2 takes everything else. Of course, you could also set the dividerLocation property before making the split pane visible. (Note that if you are using an older version of the JDK—such as 1.2.2—you have to set the preferred size.) |
I l@ve RuBoard |
![]() ![]() |