Search code examples
javaswingcolorsuimanagerjscrollbar

Change background color of ScrollBar end buttons


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.


Solution

  • 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);