Hide Left and Right Pane of a JSplitPane Programmatically in Java



In this article, we will learn how to hide the left/right pane of a JSplitPane programmatically in Java. JSplitPane is a simple Swing component in GUI programming that allows hiding one side of the split pane, resulting in a collapsible panel look.

JSplitPane

A JSplitPane is a subclass of the JComponent class that allows us to arrange two components side by side horizontally or vertically in a single pane. The display areas of both components can also be adjusted at runtime by the user.

The important methods of JSplitPane are remove(), removeAll(), resetToPreferredSizes(), and setDividerLocation(). A JSplitPane can generate a PropertyChangeListener interface.

setDividerLocation() Method

The setDividerLocation() method specifies the location of the divider. A value less than 0 is to reset the divider to a value equal to the preferred size of the left/top component.

Method Declaration:

public void setDividerLocation(int location)

getLeftComponent() Method

The getLeftComponent() method returns the component to the left (or above) of the divider.

Method Declaration:

public Component getLeftComponent()

getRightComponent() Method

The getRightComponent() method returns the component to the right (or below) of the divider.

Method Declaration:

public Component getRightComponent()

Hiding the Left/Right Pane of a JSplitPane

We can hide one of the panes (left or right) programmatically by click on the left button or right button and can generate action listeners for those buttons.

The following are the steps for hiding the left/right pane of a JSplitPane programmatically in Java:

JSplitPane Configuration

Create JButton that serve as the left/right component, leftBtn, and rightBtn. Also creates a horizontal JSplitPane named "jsp" with the two buttons.

leftBtn = new JButton("Left Button");
rightBtn = new JButton("Right Button");
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftBtn, rightBtn);
jsp.setResizeWeight(0.5);

Creating the ActionListener

Creates a shared ActionListener for both buttons, and the loc variable stores the divider position before hiding.

ActionListener actionListener = new ActionListener() {
   private int loc = 0;
   public void actionPerformed(ActionEvent ae) {
      JButton source = (JButton)ae.getSource();

Toggle Logic

When the two panes are visible, the getDividerLocation() method stores the divider location, then hides the divider by setting the setDividerSize() to 0, and displays only the side of the clicked button while hiding the other side.

if(jsp.getLeftComponent().isVisible() && jsp.getRightComponent().isVisible()) {
   loc = jsp.getDividerLocation();
   jsp.setDividerSize(0);
   jsp.getLeftComponent().setVisible(source == leftBtn);
   jsp.getRightComponent().setVisible(source == rightBtn);

Restore Visibility Logic

After one pane is hidden, we will restore the divider to its saved location by setting the getLeftComponent() and getRightComponent() visibility to true.

} else {
   jsp.getLeftComponent().setVisible(true);
   jsp.getRightComponent().setVisible(true);
   jsp.setDividerLocation(loc);
   jsp.setDividerSize((Integer) UIManager.get("SplitPane.dividerSize"));
}

Example

Below is an example of hiding the left/right pane of a JSplitPane programmatically in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSplitPaneHideTest extends JFrame {
   private JButton leftBtn, rightBtn;
   private JSplitPane jsp;
   public JSplitPaneHideTest() {
      setTitle(" JSplitPaneHide Test");
      leftBtn = new JButton("Left Button");
      rightBtn = new JButton("Right Button");
      jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftBtn, rightBtn);
      jsp.setResizeWeight(0.5);
      // Implemention code to hide left pane or right pane
      ActionListener actionListener = new ActionListener() {
         private int loc = 0;
         public void actionPerformed(ActionEvent ae) {
            JButton source = (JButton)ae.getSource();
            if(jsp.getLeftComponent().isVisible() && jsp.getRightComponent().isVisible()) {
               loc = jsp.getDividerLocation();
               jsp.setDividerSize(0);
               jsp.getLeftComponent().setVisible(source == leftBtn);
               jsp.getRightComponent().setVisible(source == rightBtn);
            } else {
               jsp.getLeftComponent().setVisible(true);
               jsp.getRightComponent().setVisible(true);
               jsp.setDividerLocation(loc);
               jsp.setDividerSize((Integer) UIManager.get("SplitPane.dividerSize"));
            }
         }
      };
      rightBtn.addActionListener(actionListener);
      leftBtn.addActionListener(actionListener);
      add(jsp, BorderLayout.CENTER);
      setSize(400, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new JSplitPaneHideTest();
   }
}

Output

The output below shows that both the left and right panes are visible:

The output below shows that when the left button is pressed, the right pane is hidden and the left is visible only:

The output below shows that when the right button is pressed, the left pane is hidden and the right is visible only:

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-15T19:38:41+05:30

464 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements