views:

282

answers:

1

I would like to remove the scrollbar arrow buttons from a scrollbar in a JScrollPane. How would I do this?

+2  A: 

If you are using the basic version of JScrollBar, then it is probably rendering using the BasicScrollBarUI. I would suggest that you extend BasicScrollBarUI to create a custom UI class (like MyBasicScrollBarUI) . The buttons are protected variables in the superclass. So you need to override the installComponents() methods in the subclass and make sure that you do not add the buttons. See the below code snippet and hide the lines as suggested there.

protected void installComponents(){
    switch (scrollbar.getOrientation()) {
    case JScrollBar.VERTICAL:
        incrButton = createIncreaseButton(SOUTH);
        decrButton = createDecreaseButton(NORTH);
        break;

    case JScrollBar.HORIZONTAL:
        if (scrollbar.getComponentOrientation().isLeftToRight()) {    
            incrButton = createIncreaseButton(EAST);
            decrButton = createDecreaseButton(WEST);
        } else {
            incrButton = createIncreaseButton(WEST);
            decrButton = createDecreaseButton(EAST);
        }
        break;
    }
    scrollbar.add(incrButton); // Comment out this line to hide arrow
    scrollbar.add(decrButton); // Comment out this line to hide arrow
    // Force the children's enabled state to be updated.
scrollbar.setEnabled(scrollbar.isEnabled());
}

Then, in your code after you initialize a JScrollBar, you can call setUI() and pass in an instance of MyBasicScrollBarUI class.

Note: I havent tried this myself, but from the code it looks like it could work.

Thimmayya
You should probably not extend the "Basic" UI. If you do then you lose all the LAF customizations. So you should probably be extending MetalScrollBarUI, or whatever UI is used for your LAF.
camickr
@camickr: agree with that. Extending basic UI was just the easiest way to illustrate a possible solution.
Thimmayya
@Thimmayya I tried the code and while the arrow icons are gone, the buttons are still there. I just edited my question to specify the removal of the button. Thanks for the idea though.
asawilliams