1. About JSplitPane
JSplitPane is a Java Swing container control, which one can use to split two other controls. The two components to be separated can also be a container like JPanel. We know a container can hold more than one components and this way we can split a container with multiple controls in it. Let us learn the working of a JSplitPane with an example.
2. JSplitPane Example
The example we are going to create is shown below:
The example shows two JTextField controls. A JSplitPane splits these two text fields vertically by running a divider horizontally. You can see the divider with a pair of arrows on the left side. These arrows help to expand the component when clicked. For example, if you click the down-arrow, top text field expands and bottom text field holds only the minimal needed space. When you click the down-arrow again, the JSplitPane hides bottom text field and shows only the top text field. This aspect is called One-Click-Expandable.
We can also adjust the size of the component by manually moving the divider. Note, the split pane can divide the component horizontally as well. Now, we will create this example and learn about the JSplitPane and in the meantime, you can watch the below YouTube video to learn the basics.
3. Split Vertical or Horizontal
In the JFrame’s constructor, we create two JTextFields and store them in the reference variable tf1
, tf2
. After this, we create the JSplitPane
. Param 2 and Param 3 denote the two components which JSplitPane will split by running the divider in between them. First param states the orientation of the splitting. In our example, we supply
VERTICAL_SPLIT constant asking JSplitPane to split the text fields vertically. This means a splitter will run horizontally between the two text fields.
1 2 3 4 5 6 7 8 9 |
//Sample 02: Create Two TextFields JTextField tf1 = new JTextField("Text Field 1"); JTextField tf2 = new JTextField("Text Field 2"); //Sample 03: Create Split Pane //3a: Test Horizontal and Vertical Split JSplitPane sp = new JSplitPane( JSplitPane.VERTICAL_SPLIT, tf1, tf2); |
Have a look at the below picture to know how the constants VERTICAL_SPLIT and HORIZONTAL_SPLIT splits the two text fields:
4. Splitter Size & Location
The method setOneTouchExpandable will accept a boolean param. In our example, we enabled it by passing the boolean true as a param to this function.
We can adjust the thickness of the splitter/divider by calling the function setDividerSize. It takes an integer to change the thickness of the splitter. The unit of this param is in pixels.
The method setDividerLocation moves the splitter to the location which is passed as a param in pixels. In our case, we specified the initial divider location as 20 from the top.
1 2 3 4 5 6 7 8 9 10 11 |
//3b: Test One Touch Expandable sp.setOneTouchExpandable(true); //3c: Set Divider Size sp.setDividerSize(10); //3d: Divider Location sp.setDividerLocation(20); //Sample 04: Add to Control Host ControlHost.add(sp); |
Below picture shows the behaviour of the Location and Size:

5. YouTube Demo: JSplitPane
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 06: Create Instance of JFrameDemo JSplitPaneExample frame = new JSplitPaneExample("JSplitPane Example"); frame.setVisible(true); } } |
6.2 JSplitPaneExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JSplitPane; import javax.swing.JTabbedPane; import javax.swing.JTextField; public class JSplitPaneExample extends JFrame { public JSplitPaneExample(String title) throws HeadlessException { //Sample 01: Set Size and Position setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); setTitle(title); //Sample 02: Create Two TextFields JTextField tf1 = new JTextField("Text Field 1"); JTextField tf2 = new JTextField("Text Field 2"); //Sample 03: Create Split Pane //3a: Test Horizontal and Vertical Split JSplitPane sp = new JSplitPane( JSplitPane.VERTICAL_SPLIT, tf1, tf2); //3b: Test One Touch Expandable sp.setOneTouchExpandable(true); //3c: Set Divider Size sp.setDividerSize(10); //3d: Divider Location sp.setDividerLocation(20); //Sample 04: Add to Control Host ControlHost.add(sp); } } |
Categories: Swing
Tags: HORIZONTAL_SPLIT, setDividerLocation, setDividerSize, setOneTouchExpandable, VERTICAL_SPLIT