2

In Java, I've been researching how to change background color of the buttons on the ends of a scroll bar. I haven't found a site that explained how and I've also looked through the UIManager Defaults to try to look for something, but I can't find anything. So if anyone can tell me how to change the background color for the buttons on a scrollbar, it would be very much appreciated. Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Josh M
  • 11,611
  • 7
  • 39
  • 49

1 Answers1

3

Create a new BasicScrollBarUI and override the createDecreaseButton and createIncreaseButton methods:

final Color newColor = ...
ScrollBarUI yourUI = new BasicScrollBarUI() {
    @Override
    protected JButton createDecreaseButton(int orientation) {
        JButton button = super.createDecreaseButton(orientation);
        button.setBackground(newColor);
        return button;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        JButton button = super.createIncreaseButton(orientation);
        button.setBackground(newColor);
        return button;
    }
};
JScrollPane scroll = ...
scroll.getVerticalScrollBar().setUI(yourUI);
scroll.getHorizontalScrollBar().setUI(yourUI);
Jeffrey
  • 44,417
  • 8
  • 90
  • 141