Set Background Color to JSplitPane in Java



In this article, we will learn to set a background color for JSplitPane in Java. JSplitPane is a Swing component that divides two (or more) components with a resizable divider. By default, JSplitPane does not directly support background color changes due to its complex structure.

What is a 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.

Syntax

The following is the syntax for a JSplitPane initialization:

JSplitPane splitPane = new JSplitPane(orientation, leftComponent, rightComponent);

Methods

The important methods of JSplitPane are:

  • remove()
  • removeAll()
  • resetToPreferredSizes()
  • setDividerLocation()

Setting Background Color on JSplitPane

A JSplitPane can generate a PropertyChangeListener interface. We can set a background color to a JSplitPane by adding two different background colors to two panels first and passing these arguments to the JSplitPane constructor.

Step-by-step process to create a JSplitPane with colored panels and a visible divider in Java:

Class Definition & Imports

The javax.swing.* provides Swing components (JSplitPane, JPanel, etc.) while the java.awt.*  provides AWT classes (Color, layout managers) and the class extends JFrame to create a window.

import javax.swing.*;
import java.awt.*;
public class JSplitPaneColorTest extends JFrame {
        }

Instance Variables

Two JPanel components, panel1 and panel2, are placed inside the split pane, and jsp is the JSplitPane that holds these two panels.

private JSplitPane jsp;
private JPanel panel1, panel2;

Initialize Panels & Set Backgrounds

The panel1 has a light gray background, and the panel2 has a Blue background.

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

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

SplitPane Configuration

JSplitPane.HORIZONTAL_SPLIT divides the pane left-right, panel1 is the left component, and panel2 is the right component. The setDividerSize(10) sets the divider thickness, and setResizeWeight(0.5) ensures that both panels start with equal width.

jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
jsp.setDividerSize(10);
jsp.setResizeWeight(0.5);

Main Method

The main method launches the application by creating an object of JSplitPaneColorTest.

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

Example

Below is an example of a JSplitPane with colored panels and a visible divider in Java:

import javax.swing.*;
import java.awt.*;
public class JSplitPaneColorTest extends JFrame {
   private JSplitPane jsp;
   private JPanel panel1,panel2;
   public JSplitPaneColorTest() {
      setTitle("JSplitPane Example");
      panel1 = new JPanel();
      panel1.setBackground(Color.lightGray);
      panel2 = new JPanel();
      panel2.setBackground(Color.blue);
      jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
      jsp.setDividerSize(10);
      jsp.setResizeWeight(0.5);
      add(jsp);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setSize(400, 275);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JSplitPaneColorTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-23T17:18:24+05:30

406 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements