Java JSplitPane

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JSplitPane

JSplitPane is used to divide two components. The two components are divided based on the look and feel implementation, and they can be resized by the user. If the minimum size of the two components is greater than the size of the split pane, the divider will not allow you to resize it.

The two components in a split pane can be aligned left to right using JSplitPane.HORIZONTAL_SPLIT, or top to bottom using JSplitPane.VERTICAL_SPLIT. When the user is resizing the components the minimum size of the components is used to determine the maximum/minimum position the components can be set to.

Nested Class

Modifier and Type Class Description
protected class JSplitPane.AccessibleJSplitPane This class implements accessibility support for the JsplitPane class.

Useful Fields

Modifier and Type Field Description
static String BOTTOM It use to add a Component below the other Component.
static String CONTINUOUS_LAYOUT_PROPERTY Bound property name for continuousLayout.
static String DIVIDER It uses to add a Component that will represent the divider.
static int HORIZONTAL_SPLIT Horizontal split indicates the Components are split along the x axis.
protected int lastDividerLocation Previous location of the split pane.
protected Component leftComponent The left or top component.
static int VERTICAL_SPLIT Vertical split indicates the Components are split along the y axis.
protected Component rightComponent The right or bottom component.
protected int orientation How the views are split.

Constructors

Constructor Description
JSplitPane() It creates a new JsplitPane configured to arrange the child components side-by-side horizontally, using two buttons for the components.
JSplitPane(int newOrientation) It creates a new JsplitPane configured with the specified orientation.
JSplitPane(int newOrientation, boolean newContinuousLayout) It creates a new JsplitPane with the specified orientation and redrawing style.
JSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) It creates a new JsplitPane with the specified orientation and redrawing style, and with the specified components.
JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) It creates a new JsplitPane with the specified orientation and the specified components.

Useful Methods

Modifier and Type Method Description
protected void addImpl(Component comp, Object constraints, int index) It cdds the specified component to this split pane.
AccessibleContext getAccessibleContext() It gets the AccessibleContext associated with this JSplitPane.
int getDividerLocation() It returns the last value passed to setDividerLocation.
int getDividerSize() It returns the size of the divider.
Component getBottomComponent() It returns the component below, or to the right of the divider.
Component getRightComponent() It returns the component to the right (or below) the divider.
SplitPaneUI getUI() It returns the SplitPaneUI that is providing the current look and feel.
boolean isContinuousLayout() It gets the continuousLayout property.
boolean isOneTouchExpandable() It gets the oneTouchExpandable property.
void setOrientation(int orientation) It gets the orientation, or how the splitter is divided.

JSplitPane Example

  1. import java.awt.FlowLayout;  
  2. import java.awt.Panel;  
  3. import javax.swing.JComboBox;  
  4. import javax.swing.JFrame;  
  5. import javax.swing.JSplitPane;  
  6. public class JSplitPaneExample {  
  7.     private static void createAndShow() {  
  8.         // Create and set up the window.  
  9.         final JFrame frame = new JFrame("JSplitPane Example");  
  10.         // Display the window.  
  11.         frame.setSize(300300);  
  12.         frame.setVisible(true);  
  13.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  14.         // set flow layout for the frame  
  15.         frame.getContentPane().setLayout(new FlowLayout());  
  16.         String[] option1 = { "A","B","C","D","E" };  
  17.         JComboBox box1 = new JComboBox(option1);  
  18.         String[] option2 = {"1","2","3","4","5"};  
  19.         JComboBox box2 = new JComboBox(option2);  
  20.         Panel panel1 = new Panel();  
  21.         panel1.add(box1);  
  22.         Panel panel2 = new Panel();  
  23.         panel2.add(box2);  
  24.         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);  
  25.         // JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,  
  26.         // panel1, panel2);  
  27.         frame.getContentPane().add(splitPane);  
  28.     }  
  29.     public static void main(String[] args) {  
  30.         // Schedule a job for the event-dispatching thread:  
  31.         // creating and showing this application's GUI.  
  32.         javax.swing.SwingUtilities.invokeLater(new Runnable() {  
  33.             public void run() {  
  34.                 createAndShow();  
  35.             }  
  36.         });  
  37.     }  
  38. }  

Output:

Java JSplitpane