17.11. The JSplitPane ClassA split pane is a special container that holds two components, each in its own sub-pane. A splitter bar adjusts the sizes of the two subpanes. In a document viewer, for example, you might use a split pane to show a table of contents next to a page of text. The following example uses two JLabels containing ImageIcons, like the previous example. It displays the two labels, wrapped in JScrollPanes, on either side of a JSplitPane (see Figure 17-10). You can drag the splitter bar back and forth to adjust the sizes of the two contained components. //file: SplitPaneFrame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class SplitPaneFrame { public static void main(String[] args) { String fileOne = "Piazza di Spagna.jpg"; String fileTwo = "L1-Light.jpg"; if (args.length > 0) fileOne = args[0]; if (args.length > 1) fileTwo = args[1]; JFrame frame = new JFrame("SplitPaneFrame"); JLabel leftImage = new JLabel( new ImageIcon( fileOne ) ); Component left = new JScrollPane(leftImage); JLabel rightImage = new JLabel( new ImageIcon( fileTwo ) ); Component right = new JScrollPane(rightImage); JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right); split.setDividerLocation(100); frame.getContentPane( ).add(split); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setSize(300, 200); frame.setVisible(true); } } Figure 17-10. Using a split pane |