How to change the color of a JSplitPane

You can use the SplitPane.background property, as shown below.

SplitPane background

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

/** @see http://stackoverflow.com/a/10110232/230513 */
public class SplitPaneDemo {

    //constructor
    public SplitPaneDemo() {
        JFrame jf = new JFrame("Split Pane Demo");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //--Make two labels to show the split pane
        JPanel left = content("Left side: ");
        JPanel right = content("Right side: ");

        //--Create a split pane
        JSplitPane jsp = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, left, right);
        jsp.setDividerLocation(0.5f);

        //Add the split pane to the frame's content pane
        jf.add(jsp);
        jf.pack();

        //Display the frame
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

        //Code to get a list of component names in the console
        for (Component myComponent : jsp.getComponents()) {
            System.out.println(myComponent);
        }
    }

    private JPanel content(String s) {
        final JLabel label = new JLabel(s + "Some text.", JLabel.CENTER);
        JPanel panel = new JPanel(new GridLayout()) {

            @Override
            public Dimension getPreferredSize() {
                Dimension d = label.getPreferredSize();
                return new Dimension(d.width * 2, d.height * 3);
            }
        };
        panel.setOpaque(true);
        panel.setBackground(new Color(0xffffffc0));
        panel.add(label);
        return panel;
    }

    public static void main(String[] args) {
        UIManager.put("SplitPane.background", new Color(0xff8080ff));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SplitPaneDemo();
            }
        });
    }
}

Leave a Comment