如何使JCombobox看起来像JTextField

时间:2021-10-14 06:47:41

Is there a good(and easy) way to make a JCombobox look like a JTextField? By this I mean there should not be a dropdown button, but when the user enters something it should show multible results.

是否有一种好的(简单的)方法可以使JCombobox看起来像JTextField?我的意思是不应该有一个下拉按钮,但是当用户输入内容时它应该显示多个结果。

Basically the same way google, youtube, facebook etc. works.

谷歌,YouTube,Facebook等基本相同的方式工作。

3 个解决方案

#1


JComboBox comboBox = new JComboBox();
comboBox.setUI(new BasicComboBoxUI() {
    @Override
    protected JButton createArrowButton() {
        return new JButton() {
            @Override
            public int getWidth() {
                return 0;
            }
        };
    }
});

Making getWidth() return 0 ensures that:
a) the button is not shown
b) no space is reserved for it, letting you type in the whole field

使getWidth()返回0确保:a)按钮未显示b)没有为它保留空间,让你输入整个字段

I found that I had to do invoke .setUI() via SwingUtilities.invokeLater(), but depending on the structure of your code, you might not have to.

我发现我必须通过SwingUtilities.invokeLater()调用.setUI(),但根据代码的结构,你可能不必这样做。

If you want autocomplete, add some items to the combo box, and use AutoCompleteDecorator.decorate(comboBox). The AutoCompleteDecorator class is part of SwingX, as previously mentioned.

如果要自动完成,请将一些项添加到组合框,并使用AutoCompleteDecorator.decorate(comboBox)。如前所述,AutoCompleteDecorator类是SwingX的一部分。

This might make your box look weird when using another L&F, so you will have to choose which CombiBoxUI to instantiate, to get the right look.

这可能会使你的盒子在使用另一个L&F时看起来很奇怪,所以你必须选择要实例化的CombiBoxUI,以获得正确的外观。

If you do not want the drop-down to appear when there is nothing in the combo box, override this method in the BasicComboBoxUI as well:

如果您不希望在组合框中没有任何内容时显示下拉列表,请在BasicComboBoxUI中覆盖此方法:

@Override
public void setPopupVisible(JComboBox c, boolean v) {
    // keeps the popup from coming down if there's nothing in the combo box
    if (c.getItemCount() > 0) {
        super.setPopupVisible(c, v);
    }
}

#2


Do you mean that you want it to behave like a text field that autocompletes against previously entered values? SwingX includes support for adding autocompletion to text components.

您的意思是您希望它的行为类似于自动填充先前输入的值的文本字段吗? SwingX支持向文本组件添加自动完成功能。

Another library you could look at is JIDE Common Layer. Their IntelliHints functionality might be what you are looking for. There is a demo you can download from here.

您可以看到的另一个库是JIDE Common Layer。他们的IntelliHints功能可能正是您所需要的。您可以从此处下载演示。

#3


I have a very similar problem, I don't care about the popup arrow, but I need to control the text appearance when the component is disabled.

我有一个非常类似的问题,我不关心弹出箭头,但我需要在禁用组件时控制文本外观。

I want to show the current value, but disable list selection/editing. The only way to get this behavior with JComboBox is to use comboBox.setEnabled(false); which makes the text an unreadable light grey.

我想显示当前值,但禁用列表选择/编辑。使用JComboBox获得此行为的唯一方法是使用comboBox.setEnabled(false);这使文本变得难以理解浅灰色。

I created a ComoBoxUIDecorator, and intercepted some of the paint methods. This has exactly the right effect -- the arrow appears greyed out while the current value appears in black and readable (as if enabled).

我创建了一个ComoBoxUIDecorator,并拦截了一些绘制方法。这具有完全正确的效果 - 箭头显示为灰色,而当前值显示为黑色且可读(如果已启用)。

public class ComboBoxUIDecorator extends ComboBoxUI {
 private ComboBoxUI m_parent;

 public ComboBoxUIDecorator(ComboBoxUI x) {
  m_parent = x;
 }
 ...
 public boolean isFocusTraversable(JComboBox c) {
  return m_parent.isFocusTraversable(c);
 }

 public void paint(Graphics g, JComponent c) {
  c.setEnabled(m_displayEnabledState);
  m_parent.paint(g, c);
  c.setEnabled(m_realEnabledState);
 }
}

You might try a similar approach; If you could find the arrow button, you could paint over the arrow after invoking m_parent.paint()

您可以尝试类似的方法;如果你能找到箭头按钮,你可以在调用m_parent.paint()之后在箭头上画画

#1


JComboBox comboBox = new JComboBox();
comboBox.setUI(new BasicComboBoxUI() {
    @Override
    protected JButton createArrowButton() {
        return new JButton() {
            @Override
            public int getWidth() {
                return 0;
            }
        };
    }
});

Making getWidth() return 0 ensures that:
a) the button is not shown
b) no space is reserved for it, letting you type in the whole field

使getWidth()返回0确保:a)按钮未显示b)没有为它保留空间,让你输入整个字段

I found that I had to do invoke .setUI() via SwingUtilities.invokeLater(), but depending on the structure of your code, you might not have to.

我发现我必须通过SwingUtilities.invokeLater()调用.setUI(),但根据代码的结构,你可能不必这样做。

If you want autocomplete, add some items to the combo box, and use AutoCompleteDecorator.decorate(comboBox). The AutoCompleteDecorator class is part of SwingX, as previously mentioned.

如果要自动完成,请将一些项添加到组合框,并使用AutoCompleteDecorator.decorate(comboBox)。如前所述,AutoCompleteDecorator类是SwingX的一部分。

This might make your box look weird when using another L&F, so you will have to choose which CombiBoxUI to instantiate, to get the right look.

这可能会使你的盒子在使用另一个L&F时看起来很奇怪,所以你必须选择要实例化的CombiBoxUI,以获得正确的外观。

If you do not want the drop-down to appear when there is nothing in the combo box, override this method in the BasicComboBoxUI as well:

如果您不希望在组合框中没有任何内容时显示下拉列表,请在BasicComboBoxUI中覆盖此方法:

@Override
public void setPopupVisible(JComboBox c, boolean v) {
    // keeps the popup from coming down if there's nothing in the combo box
    if (c.getItemCount() > 0) {
        super.setPopupVisible(c, v);
    }
}

#2


Do you mean that you want it to behave like a text field that autocompletes against previously entered values? SwingX includes support for adding autocompletion to text components.

您的意思是您希望它的行为类似于自动填充先前输入的值的文本字段吗? SwingX支持向文本组件添加自动完成功能。

Another library you could look at is JIDE Common Layer. Their IntelliHints functionality might be what you are looking for. There is a demo you can download from here.

您可以看到的另一个库是JIDE Common Layer。他们的IntelliHints功能可能正是您所需要的。您可以从此处下载演示。

#3


I have a very similar problem, I don't care about the popup arrow, but I need to control the text appearance when the component is disabled.

我有一个非常类似的问题,我不关心弹出箭头,但我需要在禁用组件时控制文本外观。

I want to show the current value, but disable list selection/editing. The only way to get this behavior with JComboBox is to use comboBox.setEnabled(false); which makes the text an unreadable light grey.

我想显示当前值,但禁用列表选择/编辑。使用JComboBox获得此行为的唯一方法是使用comboBox.setEnabled(false);这使文本变得难以理解浅灰色。

I created a ComoBoxUIDecorator, and intercepted some of the paint methods. This has exactly the right effect -- the arrow appears greyed out while the current value appears in black and readable (as if enabled).

我创建了一个ComoBoxUIDecorator,并拦截了一些绘制方法。这具有完全正确的效果 - 箭头显示为灰色,而当前值显示为黑色且可读(如果已启用)。

public class ComboBoxUIDecorator extends ComboBoxUI {
 private ComboBoxUI m_parent;

 public ComboBoxUIDecorator(ComboBoxUI x) {
  m_parent = x;
 }
 ...
 public boolean isFocusTraversable(JComboBox c) {
  return m_parent.isFocusTraversable(c);
 }

 public void paint(Graphics g, JComponent c) {
  c.setEnabled(m_displayEnabledState);
  m_parent.paint(g, c);
  c.setEnabled(m_realEnabledState);
 }
}

You might try a similar approach; If you could find the arrow button, you could paint over the arrow after invoking m_parent.paint()

您可以尝试类似的方法;如果你能找到箭头按钮,你可以在调用m_parent.paint()之后在箭头上画画