简体   繁体   中英

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

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?

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

This works for me

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM