为什么 JComboBox 忽略空值状态?
Why does the JComboBox ignore null value states?
我有一个 JComboBox
和 DefaultComboBoxModel
个 Integer
:
{null, 1, 2, 3, 4, 5, 6, 7, 8, 9}
假设我的 JComboBox
中有 5
,然后单击 JComboBox
和 select 2
,则下面的程序将显示:
5 ->
-> 2
程序如下:
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static java.lang.System.out;
public class Test {
public static void main(String[] args){
final JComboBox<Integer> cb = new JComboBox<>();
cb.setModel(new DefaultComboBoxModel<>(new Integer[]{null, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
cb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) {
case ItemEvent.DESELECTED:
out.format("%s ->\n", e.getItem());
break;
case ItemEvent.SELECTED:
out.format(" -> %s\n", e.getItem());
break;
}
}
});
final JFrame win = new JFrame();
win.setBounds(800,400,30,70);
win.add(cb);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
我不清楚的是,为什么当值最初是 null
(它是模型数组中的第一个值)时,输出是
-> 3
意味着 ItemListener
被调用了一次,只有 ItemEvent.SELECTED
状态。为什么不使用 ItemEvent.DESELECTED
来调用 null
(例如打印 null ->
),就像数字一样?
反之亦然,如果我最初在 JComboBox
中有 5
并且我 select 编辑了空值(即 null
),那么所有我在控制台看到的是:
5 ->
那么为什么 JComboBox
忽略 null
值状态? the docs 好像什么都没说。
你说的是真的,我看到的是JCombobox
class in setSelectedItem
方法:
if (anObject != null && !isEditable()) {
// For non editable combo boxes, an invalid selection
// will be rejected.
boolean found = false;
for (int i = 0; i < dataModel.getSize(); i++) {
E element = dataModel.getElementAt(i);
if (anObject.equals(element)) {
found = true;
objectToSelect = element;
break;
}
}
if (!found) {
return;
}
}
它没有设置 objectToSelect
。
所以在 DefaultComboBoxModel
实现中 setSelectedItem
方法传递 null 并且它不调用 fireContentsChanged
事件。
我有一个 JComboBox
和 DefaultComboBoxModel
个 Integer
:
{null, 1, 2, 3, 4, 5, 6, 7, 8, 9}
假设我的 JComboBox
中有 5
,然后单击 JComboBox
和 select 2
,则下面的程序将显示:
5 ->
-> 2
程序如下:
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static java.lang.System.out;
public class Test {
public static void main(String[] args){
final JComboBox<Integer> cb = new JComboBox<>();
cb.setModel(new DefaultComboBoxModel<>(new Integer[]{null, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
cb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) {
case ItemEvent.DESELECTED:
out.format("%s ->\n", e.getItem());
break;
case ItemEvent.SELECTED:
out.format(" -> %s\n", e.getItem());
break;
}
}
});
final JFrame win = new JFrame();
win.setBounds(800,400,30,70);
win.add(cb);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
win.setVisible(true);
}
}
我不清楚的是,为什么当值最初是 null
(它是模型数组中的第一个值)时,输出是
-> 3
意味着 ItemListener
被调用了一次,只有 ItemEvent.SELECTED
状态。为什么不使用 ItemEvent.DESELECTED
来调用 null
(例如打印 null ->
),就像数字一样?
反之亦然,如果我最初在 JComboBox
中有 5
并且我 select 编辑了空值(即 null
),那么所有我在控制台看到的是:
5 ->
那么为什么 JComboBox
忽略 null
值状态? the docs 好像什么都没说。
你说的是真的,我看到的是JCombobox
class in setSelectedItem
方法:
if (anObject != null && !isEditable()) {
// For non editable combo boxes, an invalid selection
// will be rejected.
boolean found = false;
for (int i = 0; i < dataModel.getSize(); i++) {
E element = dataModel.getElementAt(i);
if (anObject.equals(element)) {
found = true;
objectToSelect = element;
break;
}
}
if (!found) {
return;
}
}
它没有设置 objectToSelect
。
所以在 DefaultComboBoxModel
实现中 setSelectedItem
方法传递 null 并且它不调用 fireContentsChanged
事件。