Remove arrows from Swing Scrollbar in JScrollPane

I would like to remove the scroll arrow buttons from the scrollbar in JScrollPane. How can I do it?

+3
java swing icons jbutton jscrollbar
source share
3 answers

If you are using the basic version of JScrollBar, then this is probably rendering using BasicScrollBarUI. I would suggest you extend BasicScrollBarUI to create a user interface (e.g. MyBasicScrollBarUI). Buttons are protected variables in the superclass. Therefore, you need to override the installComponents () methods in the subclass and not add buttons. See the code snippet below 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 enabled state to be updated. scrollbar.setEnabled(scrollbar.isEnabled()); } 

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

Note. I have not tried this myself, but from the code it looks as if it could work.

+2
source share
 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 } 

}

After removing the buttons, allocate space for this. I found the buttons null as the easiest way.

+2
source share

This is not the most elegant way ... but it works for me

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

All Articles