如果启用,则隐藏 JComboBox 的按钮

发布于 2024-12-09 08:39:08 字数 634 浏览 2 评论 0原文

我想扩展 JComboBox 类,不做任何更改,我希望新组件在禁用组合框时隐藏选择按钮。

我找不到创建此按钮的位置

编辑:到目前为止我正在使用此代码:

@Override
public void setEnabled(boolean b)
{
    super.setEnabled(b);

    Component[] comps = getComponents();
    for(Component comp : comps)
    {
        if(comp instanceof MetalComboBoxButton)
        {
            final MetalComboBoxButton dropDownButton = (MetalComboBoxButton) comp;
            dropDownButton.setVisible(b);
            break;
        }
    }
}

编辑2:我最终无法做我想做的事情,就好像我切换到Nimbus PLAF,即使我隐藏按钮,背景也会被绘制,所以只有箭头被隐藏,其他一切仍然存在。

我将不得不使用 JPanel。

I would like to extends JComboBox class no change something, I want the new component to hide the selection button when the combobox is disabled.

I can't find where this button is created

EDIT : so far I am using this code :

@Override
public void setEnabled(boolean b)
{
    super.setEnabled(b);

    Component[] comps = getComponents();
    for(Component comp : comps)
    {
        if(comp instanceof MetalComboBoxButton)
        {
            final MetalComboBoxButton dropDownButton = (MetalComboBoxButton) comp;
            dropDownButton.setVisible(b);
            break;
        }
    }
}

EDIT 2 : I was unable to do what I want finally, as if I switch to Nimbus PLAF, even if I hide the button the background is drawn, so only the arrow is hidded, everything else is still there.

I will have to do with a JPanel.

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

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

发布评论

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

评论(3

病女 2024-12-16 08:39:08

从技术上讲,您可以对 JComboBox 进行子类化,然后删除/添加按钮(如 @flash 所示)或切换其可见性。

    final JComboBox box = new JComboBox(new Object[] {1, 2, 3}) {

        /** 
         * @inherited <p>
         */
        @Override
        public void setEnabled(boolean b) {
            if (b == isEnabled()) return;
            for (Component child : getComponents()) {
                if (child instanceof JButton) {
                    child.setVisible(b);
                    break;
                }
            }
            super.setEnabled(b);
        }

    };

不过,您可能需要重新考虑该要求,因为它是非标准 ui 行为 - 因此可能会让用户感到困惑

technically, you can subclass the JComboBox and either remove/add the button (as shown by @flash) or toggle its visibility

    final JComboBox box = new JComboBox(new Object[] {1, 2, 3}) {

        /** 
         * @inherited <p>
         */
        @Override
        public void setEnabled(boolean b) {
            if (b == isEnabled()) return;
            for (Component child : getComponents()) {
                if (child instanceof JButton) {
                    child.setVisible(b);
                    break;
                }
            }
            super.setEnabled(b);
        }

    };

You might want to reconsider the requirement, though, because it is non-standard ui behaviour - and as such might confuse users

十年九夏 2024-12-16 08:39:08

您可以使用如下内容:

public class CustomCombo extends JComboBox {

@Override
public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if(!enabled) {
        removeArrowButton();
    }
    else {
        addArrowButton();         
    }
}

private void removeArrowButton() {
    Component[] comp = this.getComponents();
    Component removeComponent = null;
    for (int i = 0; i < comp.length; i++) {
        if(comp[i] instanceof JButton) {
            removeComponent = comp[i];
        }
    }
    if(removeComponent != null) {
        this.remove(removeComponent);
    }
}
}

这将在您调用 customCombo.setEnabled(false) 时删除箭头按钮。

addArrowButton() 方法由您决定。这应该只是给你一个想法。

You could use something like this:

public class CustomCombo extends JComboBox {

@Override
public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if(!enabled) {
        removeArrowButton();
    }
    else {
        addArrowButton();         
    }
}

private void removeArrowButton() {
    Component[] comp = this.getComponents();
    Component removeComponent = null;
    for (int i = 0; i < comp.length; i++) {
        if(comp[i] instanceof JButton) {
            removeComponent = comp[i];
        }
    }
    if(removeComponent != null) {
        this.remove(removeComponent);
    }
}
}

This will remove the arrow button when you call customCombo.setEnabled(false).

The method addArrowButton() is left up to you. This should just give you an idea.

浅语花开 2024-12-16 08:39:08

您可能找不到它,因为您找错了地方 - 尝试 javax.swing.plaf.basic.BasicComboBoxUI.installComponents() 和 javax.swing.plaf.basic.BasicComboBoxUI。配置箭头按钮()

You may have trouble finding it because you're looking in wrong place - try javax.swing.plaf.basic.BasicComboBoxUI.installComponents() and javax.swing.plaf.basic.BasicComboBoxUI.configureArrowButton()

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