Zorion TheTenth
Zorion TheTenth

Reputation: 3

Remove Arrow buttons from JScrollBar in JScrollPane

JButton:

 public class noButton extends JButton
    {
        //tried getPreferredSize() and getSize() too
        @Override
        public Dimension getMaximumSize() 
        {          
            return new Dimension();
        }
    }

Custom UI:

public class barUI extends BasicScrollBarUI {
    barUI()
    {
    }
    protected JButton createIncreaseButton(int i) {
        return new noButton();
    }
    protected JButton createDecreaseButton(int i){
        return new noButton();
    }
    @Override
    protected void installComponents() {
        switch(this.scrollbar.getOrientation()) {
            case 0:
                if (this.scrollbar.getComponentOrientation().isLeftToRight()) {
                    this.incrButton = this.createIncreaseButton(3);
                    this.decrButton = this.createDecreaseButton(7);
                } else {
                    this.incrButton = this.createIncreaseButton(7);
                    this.decrButton = this.createDecreaseButton(3);
                }
                break;
            case 1:
                this.incrButton = this.createIncreaseButton(5);
                this.decrButton = this.createDecreaseButton(1);
        }
        this.incrButton=new noButton();
        this.decrButton=new noButton();
        this.decrButton.setSize(0,0);
        this.scrollbar.add(incrButton);
        this.scrollbar.add(decrButton);
        System.out.println(incrButton.getParent());
        System.out.println(incrButton.getSize());
        this.scrollbar.setEnabled(this.scrollbar.isEnabled());
    }
}

ScrollPane:

scroll=new JScrollPane(){
            @Override
            public void updateUI(){
                super.updateUI();
                getVerticalScrollBar().setUI(new barUI());
                getHorizontalScrollBar().setUI(new barUI());
            }
        };

I've been trying to remove one button first. The above code was suggested in various threads but it does absolutely nothing. I just get the default looking button resized by the vertical.setPreferredSize(new Dimension(10,10));

There was another suggestion to override the method that adds the buttons and remove the relevant lines.. that too did nothing..

Update : Added 'Arrow' to the question for clarification Update : Changes to the code

Upvotes: 0

Views: 555

Answers (1)

aterai
aterai

Reputation: 9833

There was another suggestion to override the method that adds the buttons and remove the relevant lines.

Seems to work OK like this:

import java.awt.*;
import java.util.Collections;
import javax.swing.*;
import javax.swing.plaf.metal.MetalScrollBarUI;

public class RemoveArrowButtonsTest {
  private Component makeUI() {
    UIManager.put("ScrollBar.incrementButtonGap", 0);
    UIManager.put("ScrollBar.decrementButtonGap", 0);
    String txt = String.join("\n", Collections.nCopies(100, "1234567890"));
    JPanel p = new JPanel(new GridLayout(1, 0));
    p.add(new JScrollPane(new JTextArea(txt)));
    p.add(new JScrollPane(new JTextArea(txt)) {
      @Override public void updateUI() {
        super.updateUI();
        getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
        getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
      }
    });
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new RemoveArrowButtonsTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class ZeroSizeButton extends JButton {
  @Override public Dimension getPreferredSize() {
    return new Dimension();
  }
}

class WithoutArrowButtonScrollBarUI extends MetalScrollBarUI {
  @Override protected JButton createDecreaseButton(int orientation) {
    return new ZeroSizeButton();
  }

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

Upvotes: 0

Related Questions