Java JSplitPane

Java JSplitPane is a Swing component that provides a way to divide a container into two resizable components. It is commonly used in user interfaces where there is a need to display two different pieces of information or views side by side and allow the user to adjust the size of each component.

JSplitPane has two main areas, left and right, that can contain any type of component, such as JTable, JTextArea, JPanel, etc. The user can resize the two components by dragging the divider between them.

To create a JSplitPane in Java, you can use the JSplitPane class and set the left and right components using the setLeftComponent() and setRightComponent() methods. Here is an example:

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                       new JScrollPane(leftComponent),
                                       new JScrollPane(rightComponent));
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(0.5);

In this example, we create a horizontal split pane with a left and right component, both wrapped in a JScrollPane. We also set the split pane to be one-touch expandable, meaning that the user can double-click on the divider to expand or collapse the two components. The setDividerLocation() method sets the initial position of the divider at 50% of the container width.

JSplitPane provides many other customization options, such as setting the orientation (horizontal or vertical), the minimum and maximum sizes of each component, and the layout of the divider. You can find more information and examples in the Java documentation.

Nested Class of Java JSplitPane:

Java JSplitPane has two nested classes:

  1. Divider – Represents the divider between the two components of the split pane. It is responsible for handling user input events like mouse dragging and setting the location of the divider.
  2. UI – Provides the look and feel for the JSplitPane component. It is responsible for rendering the split pane and its components, handling user input events, and implementing the resizing behavior of the components.

Both classes are nested inside the JSplitPane class and are not intended to be used directly by the programmer.

The Divider class can be accessed through the getDivider() method of the JSplitPane object, which returns the current Divider object. The UI class can be accessed through the getUI() method of the JSplitPane object, which returns the current UI object.

JSplitPane also provides a third nested class called BasicSplitPaneUI, which is the default implementation of the UI class. It provides a basic look and feel for the split pane and can be used as a starting point for creating custom UIs.

Nested classes are a powerful feature of Java that allows developers to organize related classes together and hide implementation details from the outside world. By nesting the Divider and UI classes inside the JSplitPane class, the implementation details of the split pane are hidden from the programmer, making it easier to use and maintain the component.

Useful Fields of Java JSplitPane:

Java JSplitPane provides several useful fields that allow developers to customize the behavior and appearance of the component. Here are some of the most commonly used fields:

  1. HORIZONTAL_SPLIT: A static field that specifies the horizontal orientation of the split pane. This field is used when creating a new JSplitPane instance to set its orientation to horizontal.
  2. VERTICAL_SPLIT: A static field that specifies the vertical orientation of the split pane. This field is used when creating a new JSplitPane instance to set its orientation to vertical.
  3. ONE_TOUCH_EXPANDABLE_PROPERTY: A static field that specifies whether the split pane should have a one-touch expandable button. This field can be set using the putClientProperty() method and its value should be a Boolean object.
  4. DIVIDER_LOCATION_PROPERTY: A static field that specifies the location of the divider between the left and right components of the split pane. This field can be set using the putClientProperty() method and its value should be an Integer object representing the pixel location of the divider.
  5. DIVIDER_SIZE_PROPERTY: A static field that specifies the size of the divider. This field can be set using the putClientProperty() method and its value should be an Integer object representing the pixel size of the divider.
  6. LAST_DIVIDER_LOCATION_PROPERTY: A static field that specifies the last known location of the divider. This field can be used to restore the position of the divider after the split pane has been resized.
  7. RESIZE_WEIGHT_PROPERTY: A static field that specifies the resize weight of the split pane. This field can be set using the putClientProperty() method and its value should be a Double object between 0.0 and 1.0, representing the proportion of space given to the left component.

These fields can be accessed and set using the putClientProperty() and getClientProperty() methods of the JSplitPane object. They provide a way to customize the behavior and appearance of the split pane without having to subclass the component.

Constructors of Java JSplitPane:

Java JSplitPane provides several constructors to create instances of the component. Here are some of the most commonly used constructors:

  1. JSplitPane() – Creates a new split pane with default orientation (horizontal), no left or right components, and a default divider location.
  2. JSplitPane(int orientation) – Creates a new split pane with the specified orientation (horizontal or vertical), no left or right components, and a default divider location.
  3. JSplitPane(int orientation, Component leftComponent, Component rightComponent) – Creates a new split pane with the specified orientation, left and right components, and a default divider location.
  4. JSplitPane(int orientation, boolean continuousLayout, Component leftComponent, Component rightComponent) – Creates a new split pane with the specified orientation, left and right components, and a default divider location. The continuousLayout parameter determines whether the components are continuously laid out as the divider is being dragged.
  5. JSplitPane(int orientation, boolean continuousLayout, Component leftComponent, Component rightComponent, int dividerSize) – Creates a new split pane with the specified orientation, left and right components, a default divider location, and a custom divider size.
  6. JSplitPane(int orientation, boolean continuousLayout, Component leftComponent, Component rightComponent, float resizeWeight) – Creates a new split pane with the specified orientation, left and right components, a default divider location, and a custom resize weight.

The orientation parameter can be either JSplitPane.HORIZONTAL_SPLIT or JSplitPane.VERTICAL_SPLIT. The leftComponent and rightComponent parameters are the two components that will be displayed on the left and right sides of the divider, respectively. The continuousLayout parameter determines whether the components are continuously laid out during a divider drag. The dividerSize parameter specifies the width or height (depending on orientation) of the divider. The resizeWeight parameter determines the proportion of space given to the left component.

These constructors provide a convenient way to create instances of the JSplitPane component with different configurations, without having to manually set each property.

Useful Methods of Java JSplitPane:

Java JSplitPane provides many useful methods that allow developers to interact with and customize the behavior of the component. Here are some of the most commonly used methods:

  1. setLeftComponent(Component leftComponent) and setRightComponent(Component rightComponent) – Sets the left and right components of the split pane.
  2. getLeftComponent() and getRightComponent() – Returns the left and right components of the split pane.
  3. setDividerLocation(int location) and getDividerLocation() – Sets and gets the location of the divider between the left and right components.
  4. setDividerSize(int size) and getDividerSize() – Sets and gets the size of the divider.
  5. setOneTouchExpandable(boolean expandable) and isOneTouchExpandable() – Sets and gets whether the split pane should have a one-touch expandable button.
  6. setResizeWeight(double weight) and getResizeWeight() – Sets and gets the resize weight of the split pane.
  7. setContinuousLayout(boolean continuousLayout) and isContinuousLayout() – Sets and gets whether the components are continuously laid out as the divider is being dragged.
  8. resetToPreferredSizes() – Sets the divider location to the preferred location of the left or right component.
  9. setOrientation(int orientation) and getOrientation() – Sets and gets the orientation of the split pane.
  10. setTopComponent(Component topComponent) and setBottomComponent(Component bottomComponent) – Sets the top and bottom components of the split pane for vertical split orientation.
  11. getTopComponent() and getBottomComponent() – Returns the top and bottom components of the split pane for vertical split orientation.

These methods provide a way to interact with and customize the behavior of the split pane, allowing developers to create split panes that suit their specific needs.

JSplitPane Example:

Here is an example of how to use JSplitPane in a Java Swing application:

import javax.swing.*;
import java.awt.*;

public class MySplitPane extends JFrame {

    public MySplitPane() {
        super("JSplitPane Example");
        setSize(500, 400);

        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.RED);

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.BLUE);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(200);

        getContentPane().add(splitPane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MySplitPane();
    }
}

In this example, we create a JFrame and set its size to 500×400. We then create two JPanel components with different background colors and set them as the left and right components of a JSplitPane. We set the orientation of the split pane to JSplitPane.HORIZONTAL_SPLIT, set the one-touch expandable feature to true, and set the initial divider location to 200 pixels. Finally, we add the split pane to the content pane of the JFrame and make the JFrame visible.

When the application is run, a split pane with two panels is displayed. The user can drag the divider to resize the panels, and can use the one-touch expandable button to quickly expand or collapse one of the panels.