隐藏 JComBox 框箭头

发布于 2024-12-05 09:53:48 字数 139 浏览 4 评论 0原文

是否可以隐藏 JComboBox 中显示的箭头

我尝试设置:

combo.getComponent(0).setSize(new Dimension(1,1));

但它似乎不起作用

Is it possible to hide the arrow displayed in the JComboBox

I tried setting:

combo.getComponent(0).setSize(new Dimension(1,1));

But it doesnt seem to work

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

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

发布评论

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

评论(2

神爱温柔 2024-12-12 09:53:48

您必须为此创建一个新的组合框 UI:

combo.setUI(new BasicComboBoxUI() {
    protected JButton createArrowButton() {
        return new JButton() {
            public int getWidth() {
                return 0;
            }
        };
    }
});

但要小心从与您当前的外观和感觉相匹配的基本 UI 继承。

例如,如果您使用 Substance,则应从 SubstanceComboBoxUI 而不是 BasicComboBoxUI 派生新 UI。否则,您可能会失去当前 L&F 提供的功能。

编辑:如果您希望获得某种自动完成功能,最好坚持使用普通的JTextField并使用中的AutoCompleteDecorator href="http://swingx.java.net/" rel="noreferrer">SwingX

You have to create a new combobox UI for that:

combo.setUI(new BasicComboBoxUI() {
    protected JButton createArrowButton() {
        return new JButton() {
            public int getWidth() {
                return 0;
            }
        };
    }
});

But be careful to inherited from the base UI which matches your current look and feel.

For example if you are using Substance you should derive your new UI from SubstanceComboBoxUI instead of BasicComboBoxUI. Otherwise you'll might loose features provided by your current L&F.

EDIT: If you want this to get some kind of auto-completion feature it's better to stick with a normal JTextField and use AutoCompleteDecorator from SwingX.

不必你懂 2024-12-12 09:53:48

我一直在寻找解决方案,事实证明,真正需要做的就是记住 JComboBox 是一个复合组件。

for (Component component : TheComboBox.getComponents())
{
    if (component instanceof JButton) {
        TheComboBox.remove(component);
    }
}

感谢 mKorbel 提供的 提醒

I've been looking for a solution to this for a while now, and it turns out that all it really takes is remembering that JComboBox is a compound component.

for (Component component : TheComboBox.getComponents())
{
    if (component instanceof JButton) {
        TheComboBox.remove(component);
    }
}

Thanks go to mKorbel for the reminder.

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