Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JSplitPane – Splitting the Components

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:

JSplitPane Example
JSplitPane Example

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.

Video1: JSplitPane 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.

Have a look at the below picture to know how the constants VERTICAL_SPLIT and HORIZONTAL_SPLIT splits the two text fields:

Verical and Horizontal Split
Verical and Horizontal Split

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.

Below picture shows the behaviour of the Location and Size:

Splitter Size & Location
Splitter Size & Location

5. YouTube Demo: JSplitPane

Code Implementation & Demo

6. Code Reference

6.1 MainEntry.java

6.2 JSplitPaneExample.java

Categories: Swing

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.