JSplitPane in Java with Example using Netbeans

JSplitPane in java

JSplitPane in java with Example using NetBeans- The javax.swing.JSplitPane class provides a two-part container. The JSplitPane is often used to separate menu navigation and content, but there are also other areas of application.

Windows Explorer is an example of a window split into two, which can also be realized with a JSplitPane. There you can select the folder on the left side, while the corresponding files of the selected directory are displayed on the right side.

The JSplitPane allows both a horizontal and a vertical split. The division can either be fixed or designed flexibly.

If a JSplitPane is created using the parameterless standard constructor, the two panes are arranged horizontally next to each other. The value for newContinuousLayout is set to false by default, which means that when the divider is moved, the two sections are not redrawn until the move is complete, i.e. the mouse button is released.

However, there are other constructors that can be used to influence the alignment and layout of the JSplitPanes.

JSplitPane Constructors:

constructor Description
JSplitPane(int newOrientation The newOrientation parameter can be used to set whether the separation should be horizontal or vertical. The JSplitPane class offers the constants JSplitPane.HORIZONTAL_SPLIT and JSplitPane.VERTICAL_SPLIT for this
JSplitPane(int newOrientation, boolean newContinuousLayout) Here you can use the second parameter to control whether the contents of the two halves should be continuously updated during the movement of the divider (true) or only after the complete movement of the divider has ended.
JSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) With this constructor, the components for both sides can be passed at the same time.
JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) Similar to the constructor above, but without the newContinuousLayout parameter, which is automatically set to false here.

Example: how to create a JSplitPane with a horizontal split in java:

package com.mycompany.guiexamples;

import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

 * @author Fawadkhan
public class SplitPaneExample {

    public static void main(String[] args) {
        // creation of a new 
        JDialog myJDialog = new JDialog();
        myJDialog.setTitle("JSplitPane Example");
        myJDialog.setSize(450, 300);

        // create two JPanel objects 
        JPanel panelGreen = new JPanel();
        JPanel panelCyan = new JPanel();
        // Background colors of the JPanels are set 
      //Create labels for the two sides 
        JLabel labelGreen = new JLabel("Programming Digest");
        JLabel labelCyan = new JLabel("JSplitePane Example");
        //Labels are added to our panels 

        // Creation of a JSplitPane object with horizontal split 
        JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        // Here we put our green JPanel on the left and the cyan on the right 

        // Here we add our JSplitPane to our dialog 
        // We display our dialog 



First, I create two JPanes, which I  give a green and cyan background color and our own JLabel for better illustration. Then I create a JSplitPane object using the first constructor as you can see in the above constructor table. Finally, I set our green color JPanel to the left using the setLeftComponent method and set our cyan color JPanel to the right using the setRightComponent method.

When you run the above code you will see the interface like this:


You may be surprised that the dividing line is vertical, even though we passed the constant JSplitPane.HORIZONTAL_SPLIT to the constructor. However, the term horizontal refers to the orientation of the two sides. In the case of horizontal alignment, the two sides are arranged side by side and in the case of vertical alignment, one above the other.

In the following we would like to explain some selected methods of the JSplitPane class:

JSplitPane Methods:

Method Description
Component getBottomComponent() This method returns the right or bottom container. The method  getRightComponent offers the same functionality.
Component getLeftComponent() This method returns the left and top containers respectively. The method getTopComponent offers the same functionality.
void setDividerLocation(int location) This method allows you to set the position of the separator.
void setBottomComponent(Component comp) This method sets the right or bottom container. The method setRightComponent offers the same functionality.
void setLeftComponent(Component comp) This method sets the left or top container. The method  setTopComponent offers the same functionality.


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button