问题
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