从 JScrollPane 中的 Swing 滚动条中删除箭头

发布于 2024-08-12 04:52:41 字数 45 浏览 10 评论 0原文

我想从 JScrollPane 中的滚动条中删除滚动条箭头按钮。我该怎么做?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猥︴琐丶欲为 2024-08-19 04:52:41
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
}

删除

按钮为接下来留出空间。我发现将按钮归零是最简单的方法。

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.

单身狗的梦 2024-08-19 04:52:41

如果您使用的是 JScrollBar 的基本版本,那么它可能是使用 BasicScrollBarUI 进行渲染的。我建议您扩展 BasicScrollBarUI 来创建自定义 UI 类(如 MyBasicScrollBarUI)。这些按钮是超类中受保护的变量。因此,您需要重写子类中的 installComponents() 方法,并确保不添加按钮。请参阅下面的代码片段并按照那里的建议隐藏这些行。

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());
}

然后,在初始化 JScrollBar 后的代码中,您可以调用 setUI() 并传入 MyBasicScrollBarUI 类的实例。

注意:我自己还没有尝试过,但从代码来看它似乎可以工作。

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.

只涨不跌 2024-08-19 04:52:41

这不是最优雅的方式......但对我有用

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

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

JScrollBar jsb = getHorizontalScrollBar();
        for(Component c : jsb.getComponents()) {
            jsb.remove(c);
        }
野侃 2024-08-19 04:52:41

这就是我走过的路。

  1. 将要隐藏的滚动条的滚动条策略设置为 never
  2. 使用 MouseWheelListener 模仿此行为

此方法:

  • 只需很少的代码即可快速实现。
  • 保留了L&F 的优点。
  • 删除按钮和栏。

下面是删除垂直滚动条示例代码。

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

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);
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文