How to change the color or background color of JSplitPane divider?

7.7k Views Asked by At

I am trying to the set the background color for the JSplitPane divider. I've written the following code, but it doesn't seem to work.

        BasicSplitPaneUI ui = (BasicSplitPaneUI) splitPane.getUI();
        BasicSplitPaneDivider divider = ui.getDivider();
        divider.setBackground(Color.decode("#FFFACD"));

I've even tried the suggestion given here How to set BackGround color to a divider in JSplitPane

Can someone please point out the mistake or let me know any other approach?

3

There are 3 best solutions below

1
On BEST ANSWER

This works for me

BasicSplitPaneDivider divider = (BasicSplitPaneDivider) splitPane.getComponent(2);
divider.setBackground(Color.black);
divider.setBorder(null);
0
On

I searched for many post for changing the divider color of split pane. And i did found the solution for it.

splitPane.setUI(new BasicSplitPaneUI() 
{
    @Override
    public BasicSplitPaneDivider createDefaultDivider() 
    {
        return new BasicSplitPaneDivider(this) 
        {                
            public void setBorder(Border b) {}

            @Override
            public void paint(Graphics g) 
            {
                g.setColor(Color.BLACK);
                g.fillRect(0, 0, getSize().width, getSize().height);
                super.paint(g);
            }
        };
    }
});

splitPane.setBorder(null);

With the above code,we can set the color,set the border for the divider too.For more information,refer this tutorial

0
On

What Look and Feel are you using? The LaF can and often does override what you may set.

This may help http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/color.html