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

无人久伴 提交于 2019-12-23 17:25:14

问题


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?


回答1:


This works for me

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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/8934169/how-to-change-the-color-or-background-color-of-jsplitpane-divider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!