Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   1. Advanced GUI Features  >   1.4. Split panes

1.4. Split panes
Last updated: 1 January 2013.

This tutorial will show you how to use split panes in Java.

A split pane is an instance of the class JSplitPane. It provides a vertical or horizontal divider between two components. The divider can be dragged to shrink or enlarge the display area of either of the two components. If the user shrinks a component's display area, a portion of that component might be completely hidden. Therefore, when creating a split pane, it is recommended to wrap scroll panes around each of its two components. The following figure shows a frame which contains a horizontal split pane separating two panels:

Split pane example

Here is the code that builds the frame shown above:

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class SplitPanes extends JFrame {

public SplitPanes(){


private void addComponents() {
// Set up a custom font
Font font = new Font("Times New Roman", Font.ITALIC, 75);

// Set up the top panel and wrap a scroll pane around it
JPanel topPanel = new JPanel();
          JLabel topLabel =
new JLabel("Top Panel", SwingConstants.CENTER);
          JScrollPane topScrollPane =
new JScrollPane(topPanel);

// Set up the bottom panel and wrap a scroll pane around it
JPanel bottomPanel = new JPanel();
          JLabel bottomLabel =
new JLabel("Bottom Panel", SwingConstants.CENTER);
          JScrollPane bottomScrollPane =
new JScrollPane(bottomPanel);

// Create the split pane
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topScrollPane, bottomScrollPane);

// Allow the user to expand/collapse either of the two components

// Set the location of the split pane's divider

// Add the split pane to the JFrame

private void init() {
catch(Exception e){

(500, 250);

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SplitPanes();


As you can see, I used the constructor JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent). The possible values for the argument newOrientation are JSplitPane.HORIZONTAL_SPLIT and JSplitPane.VERTICAL_SPLIT.

A split pane can be used with only two components. In the code above, I passed in two JScrollPane instances to the JSplitPane constructor. Instead of passing in JScrollPane instances, you can pass in other JSplitPane instances to the constructor and thereby get nested split panes.

The call setOneTouchExpandable(true) displays two small arrows at the edge of the divider. With these arrows, you can expand or collapse either of the two components.

Finally, the call setDividerLocation(0.5) places the divider in the middle of the split pane's display area. The argument 0.5 specifies that the divider will be located at 50% of the split pane's size.

Note that if you set the minimum size of a component within a split pane, that component cannot be shrinked to a size smaller than its minimum size. Still, the small arrows at the edge of the divider can collapse/expand either of the two components within a split pane, regardless of their minimum sizes.

You are here :  JavaPerspective.com  >   Advanced Tutorials  >   1. Advanced GUI Features  >   1.4. Split panes
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   1. Advanced GUI Features  >   1.5. Text areas

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7