Java Swing Tutorial - JSplitPane








A JSplitPane has a splitter to split two components. The splitter bar can be displayed horizontally or vertically.

The user can move the splitter bar up/down or left/right, so one component gets more space than the other.

The JSplitPane class provides many constructors. we can create it using its default constructor and add two components using its setTopComponent(Component c), setBottomComponent(Component c),setLeftComponent(Component c), setRightComponent(Component c).

JSplitPane can redraw components in continuous or non-continuous way. when we change the position of the splitter bar.

Reset the divider position to that position by calling the resetToPreferredSizes() method of JSplitPane.

Divider Location

Change the dividerLocation property with setDividerLocation(newLocation). 0.0 and 1.0, representing a percentage of the JSplitPane container width.

import java.awt.BorderLayout;
/*  w w w.j  a  v  a  2s. c o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);

    

    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);

    splitPane.setDividerLocation(0.5);
  }
}




Orientation

Setting Orientation: JSplitPane.VERTICAL_SPLIT or JSplitPane.HORIZONTAL_SPLIT

import java.awt.BorderLayout;
/*from   ww  w .j  a v  a 2 s .c  o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    JComponent leftButton = new JButton("Left");
    JComponent rightButton = new JButton("Right");
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setLeftComponent(leftButton);
    splitPane.setRightComponent(rightButton);
    
    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);
    
  }
}




Expandable Divider

Resizing Components and Working with a One-Touch Expandable Divider

import java.awt.BorderLayout;
/*from   w  ww.jav  a  2 s. c  o  m*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

Nested JSplitPane

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
//  w w  w  .  j  a  v  a2  s.c  o m
public class Main{
  
  public static void main(String[] a) {
    int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT;

    int VERTSPLIT = JSplitPane.VERTICAL_SPLIT;

    boolean continuousLayout = true;


    JLabel label1 = new JLabel("a");
    JLabel label2 = new JLabel("b");
    JLabel label3 = new JLabel("c");
    JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, label1, label2);
    splitPane1.setOneTouchExpandable(true);
    splitPane1.setDividerSize(2);
    splitPane1.setDividerLocation(0.5);

    JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, label3);
    splitPane2.setOneTouchExpandable(true);
    splitPane2.setDividerLocation(0.4);
    splitPane2.setDividerSize(2);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(splitPane2);
    frame.pack();
    frame.setVisible(true);
  }
}

Listening for JSplitPane Property Changes

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
// w  w  w . j a  va2s .  co m
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent changeEvent) {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
          int current = sourceSplitPane.getDividerLocation();
          System.out.println("Current: " + current);
          Integer last = (Integer) changeEvent.getNewValue();
          System.out.println("Last: " + last);
          Integer priorLast = (Integer) changeEvent.getOldValue();
          System.out.println("Prior last: " + priorLast);
        }
      }
    };

    splitPane.addPropertyChangeListener(propertyChangeListener);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}