JComboBox 中的 Null 停止箭头键的使用

发布于 2024-10-13 08:18:04 字数 913 浏览 4 评论 0原文

下面的代码有一个错误。加载 JFrame 后,按 Tab 键将焦点放在 JComboBox 上,然后尝试按向下键。它没有任何作用。

在位置 0 处插入 Null 会导致这种情况。但是,我仍然希望能够选择 Null。我不想强迫用户选择一个选项。

package kobalt.test.colin;

import java.awt.*;
import javax.swing.*;

public class ColinTest extends JFrame {

    private JTextField mTextField;
    private JComboBox  mComboBox;

    public ColinTest(){
      setLayout(new BorderLayout());

      mTextField = new JTextField("Something");
      mComboBox = new JComboBox(new String[]{"One", "Two"});
      mComboBox.insertItemAt(null, 0); //this line causes the bug

      add(mTextField, "North");
      add(mComboBox, "South");

      pack();
      setVisible(true);
    }


    public static void main(String[] argv) {
      new ColinTest();
    }
}

我可以在 JComboBox 中重写某些内容来修复此行为吗?

我真的不喜欢在位置 0 处插入一个空字符串,因为我必须在任何地方处理这个问题。

使用 Wrapping 对象可能是一种选择,但我宁愿扩展然后覆盖 JComboBox 中的某些内容。

The code below has a bug. Upon loading the JFrame, press tab to focus on the JComboBox, and try pressing the down key. It doesn't do anything.

Inserting Null at position 0 causes this. However, I would still like to be able to select Null. I don't want to force the user to pick an option.

package kobalt.test.colin;

import java.awt.*;
import javax.swing.*;

public class ColinTest extends JFrame {

    private JTextField mTextField;
    private JComboBox  mComboBox;

    public ColinTest(){
      setLayout(new BorderLayout());

      mTextField = new JTextField("Something");
      mComboBox = new JComboBox(new String[]{"One", "Two"});
      mComboBox.insertItemAt(null, 0); //this line causes the bug

      add(mTextField, "North");
      add(mComboBox, "South");

      pack();
      setVisible(true);
    }


    public static void main(String[] argv) {
      new ColinTest();
    }
}

Is there something I can override in JComboBox to fix this behaviour?

I don't really fancy inserting an empty String at position 0 as I'll have to deal with that everywhere.

Using a Wrapping object may be an option, but I'd rather extend and then override something in JComboBox.

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

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

发布评论

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

评论(2

一花一树开 2024-10-20 08:18:04

空对象在 JComboBox 中表现不佳。例如,当您选择某个项目时会触发组合框的 getSelectedIndex 方法,如果该对象为 null,则该方法将返回 -1。还可能有其他方法执行空检查并可能返回不正确的结果。

但是您可以尝试重写 getSelectedIndex ,以便在对象为 null 时返回 0 而不是 -1。还要重写selectedItemChanged,以便它不检查空值。以下似乎可行,但可能还有其他方法也需要重写:

JComboBox mComboBox = new JComboBox(new String[]{"One", "Two"}){
    @Override
    public int getSelectedIndex() {
        Object sObject = dataModel.getSelectedItem();
        int i,c;
        Object obj;

        if(sObject==null){
            return 0;
        }
        for ( i=0,c=dataModel.getSize();i<c;i++ ) {
            obj = dataModel.getElementAt(i);

             if ( obj != null && obj.equals(sObject) )
                return i;
        }
        return -1;
    }

    @Override
    protected void selectedItemChanged() {
        fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                selectedItemReminder,
                ItemEvent.DESELECTED));
        selectedItemReminder = dataModel.getSelectedItem();

        fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                selectedItemReminder,
                ItemEvent.SELECTED));
    }
};

但是,我建议使用包装对象,而不是执行上述操作。例如:

class StringWrapper{
    final String s;
    public StringWrapper(String s){
        this.s=s;
    }
    @Override
    public String toString() {
        return s;
    }
}

JComboBox cb = new JComboBox(new StringWrapper[]{ 
            new StringWrapper("one"),
            new StringWrapper("two"),
            new StringWrapper("three")});
cb.insertItemAt(new StringWrapper(null), 0); 

Null objects do not play nicely in a JComboBox. For example, the combo box's getSelectedIndex method, which is fired when you select an item, will return -1 if the object is null. There may also be other methods which perform null checks and may return incorrect results.

But you can try overriding the getSelectedIndex so that it returns 0 instead of -1 if the object is null. Also override selectedItemChanged so that it doesn't check for nulls. The following seems to work, but there may be other methods which need to be overridden too:

JComboBox mComboBox = new JComboBox(new String[]{"One", "Two"}){
    @Override
    public int getSelectedIndex() {
        Object sObject = dataModel.getSelectedItem();
        int i,c;
        Object obj;

        if(sObject==null){
            return 0;
        }
        for ( i=0,c=dataModel.getSize();i<c;i++ ) {
            obj = dataModel.getElementAt(i);

             if ( obj != null && obj.equals(sObject) )
                return i;
        }
        return -1;
    }

    @Override
    protected void selectedItemChanged() {
        fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                selectedItemReminder,
                ItemEvent.DESELECTED));
        selectedItemReminder = dataModel.getSelectedItem();

        fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                selectedItemReminder,
                ItemEvent.SELECTED));
    }
};

However, instead of doing the above, I would recommend using a wrapper object. For example:

class StringWrapper{
    final String s;
    public StringWrapper(String s){
        this.s=s;
    }
    @Override
    public String toString() {
        return s;
    }
}

JComboBox cb = new JComboBox(new StringWrapper[]{ 
            new StringWrapper("one"),
            new StringWrapper("two"),
            new StringWrapper("three")});
cb.insertItemAt(new StringWrapper(null), 0); 
晒暮凉 2024-10-20 08:18:04

以这种方式创建组合框会创建基于 Vector 的 DefaultComboBoxModel。所以不能插入空值。您可以尝试使用 null 支持来实现 ComboBoxModel。

Creation of combobox in such a way creates DefaultComboBoxModel which is based on Vector. So nulls can't be inserted. You can try to implement your ComboBoxModel with nulls support.

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