如果启用,则隐藏 JComboBox 的按钮
我想扩展 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术上讲,您可以对 JComboBox 进行子类化,然后删除/添加按钮(如 @flash 所示)或切换其可见性。
不过,您可能需要重新考虑该要求,因为它是非标准 ui 行为 - 因此可能会让用户感到困惑
technically, you can subclass the JComboBox and either remove/add the button (as shown by @flash) or toggle its visibility
You might want to reconsider the requirement, though, because it is non-standard ui behaviour - and as such might confuse users
您可以使用如下内容:
这将在您调用
customCombo.setEnabled(false)
时删除箭头按钮。addArrowButton()
方法由您决定。这应该只是给你一个想法。You could use something like this:
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.您可能找不到它,因为您找错了地方 - 尝试 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()
andjavax.swing.plaf.basic.BasicComboBoxUI.configureArrowButton()