JSplitPane
The JSplitPane class displays two components next to each other with a user moveable dividing bar between them. Usually, the components on either side of the divider will be panels, each containing one of more other components:
// Panel to go on the left or the top
JPanel panelOne = new JPanel();
panelOne.add(new JLabel("This is panel 1"));
// Panel to go on the right or the bottom
JPanel panelTwo = new JPanel();
panelTwo.add(new JLabel("This is panel 2"));
// Split the panels
JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelOne, panelTwo);
panel.add(splitter);
The JSplitPane constructor needs three arguments:
- Whether the split should be horizontal or vertical
- The component that should be placed to the left (or top)
- The component that should be placed to the right (or bottom)
Running the above will look as follows:
Notice the dividing bar in the centre of the frame. At the moment it will not move because each of the two panels on either side are at their minimum dimensions and they have not been made scrollable. Enlarge the frame window by dragging the bottom right corner of the frame so it looks as follows:
Now you will be able to drag the centre dividing bar so that it appears in another location:
You can obtain a vertical split simply by modifying the constructor's first argument value:
JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panelOne, panelTwo);
Comments