我正在做这个项目,我试图在组合框中打印出所选项目,但它无法正常工作。只是想知道为什么它没有正确打印出来。试图打印" eric white"
public void subList() {
//sets up sub list based on selection in managerbox
cboManager.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
Object selected = cboManager.getSelectedItem();
if (selected.equals("Eric White") ) {
System.out.println("eric white");
}
}
}
});
}
答案 0 :(得分:0)
检查组合框中列出的项目是否完全是“Eric White”,因为equals将查找包含空格和区分大小写的精确字符串。
答案 1 :(得分:0)
我通常使用ActionListener
。准备null
值和区分大小写。
public void subList() {
//sets up sub list based on selection in managerbox
cboManager.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object selected = cboManager.getSelectedItem();
if (selected != null && selected.equalsIgnoreCase("Eric White") ) {
System.out.println("eric white");
}
}
});
}