JComboBox 中的 Null 停止箭头键的使用
下面的代码有一个错误。加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
空对象在 JComboBox 中表现不佳。例如,当您选择某个项目时会触发组合框的
getSelectedIndex
方法,如果该对象为null
,则该方法将返回 -1。还可能有其他方法执行空检查并可能返回不正确的结果。但是您可以尝试重写
getSelectedIndex
,以便在对象为 null 时返回 0 而不是 -1。还要重写selectedItemChanged
,以便它不检查空值。以下似乎可行,但可能还有其他方法也需要重写:但是,我建议使用包装对象,而不是执行上述操作。例如:
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 isnull
. 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 overrideselectedItemChanged
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:However, instead of doing the above, I would recommend using a wrapper object. For example:
以这种方式创建组合框会创建基于 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.