3

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

mKorbel
  • 108,320
  • 17
  • 126
  • 296
asawilliams
  • 2,838
  • 2
  • 26
  • 53

4 Answers4

3
class NoArrowScrollBarUI extends BasicScrollBarUI {


protected JButton createZeroButton() {
    JButton button = new JButton("zero button");
    Dimension zeroDim = new Dimension(0,0);
    button.setPreferredSize(zeroDim);
    button.setMinimumSize(zeroDim);
    button.setMaximumSize(zeroDim);
    return button;
}

@Override
protected JButton createDecreaseButton(int orientation) {
    return createZeroButton();
}

@Override
protected JButton createIncreaseButton(int orientation) {
    return createZeroButton();
}


@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
    //own painting if needed
}

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    //own painting if needed
}

}

Removing buttons let space for then. I found make buttons zero as the simplest way.

honzik
  • 31
  • 2
2

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
  • 2,004
  • 2
  • 18
  • 19
  • 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 Nov 24 '09 at 03:39
  • @camickr: agree with that. Extending basic UI was just the easiest way to illustrate a possible solution. – Thimmayya Nov 24 '09 at 03:45
  • @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 Nov 24 '09 at 16:12
  • The code just hides the buttons. The place holder space for buttons still remains there and remains vacant. It is not occupied by the scroll bar. – tiger Oct 03 '11 at 09:38
0

It is not the most elegant way... but works for me

JScrollBar jsb = getHorizontalScrollBar();
        for(Component c : jsb.getComponents()) {
            jsb.remove(c);
        }
0

This is the way i went.

  1. Set the scrollbar policy of the scrollbar you want to hide as never
  2. Mimic this behavior with a MouseWheelListener

This method:

  • Is fast to implement with very few lines of code.
  • Retains the benefits of the L&F.
  • Will remove both the buttons and the bar.

Below is a sample code for removing the verticall scroll bar.

JScrollPane myScrollPane = new JScrollPane();

//remove the scroll bar you don't want
myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

JTextPane myJTextArea = new JTextPane();

//myScrollPane.setViewportView(myJTextArea);

myScrollPane.addMouseWheelListener(new MouseWheelListener() {

   //this will mimick the behavior of scrolling
   public void mouseWheelMoved(MouseWheelEvent e) {
            JScrollBar scrollBar =  myScrollPane.getVerticalScrollBar();
            //capturing previous value
            int previousValue = scrollBar.getValue();

            int addAmount;

            //decide where the wheel scrolled
            //depending on how fast you want to scroll
            //you can chane the addAmount to something greater or lesser

            if(e.getWheelRotation()>0) {
                addAmount = 2;
            }else {
                addAmount = -2;
            }

            //set the new value 
            scrollBar.setValue(previousValue + addAmount);
        }
    });
Kostas Thanasis
  • 183
  • 2
  • 11