The JSplitPane Class

The 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.

Simple JSplitPane with two text areas

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( ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.