AutoCompleteDecorator正在中断ItemListener [英] AutoCompleteDecorator is interrupting ItemListener
问题描述
我有一个与 AutoCompleteDecorator $ c>集成的
可编辑
JComboBox
$ c> of SwingX
库。我的 JComboBox
也有一个 ItemListener
注册。现在,请查看下面的代码。
I have an editable
JComboBox
which is integrated with the AutoCompleteDecorator
of SwingX
library. My JComboBox
is also having an ItemListener
registered to it as well. Now, Please have a look at the below code.
AutoCompleteDecorator.decorate(ClientNameCombo);
ClientNameCombo.addItemListener(new ClientNameComboAction());
private class ClientNameComboAction implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
String selectedClientName= ClientNameCombo.getSelectedItem().toString();
if(!selectedClientName.equals("Select Client"))
{
int idClient = Integer.parseInt(String.valueOf(client_name_id_map.get(selectedClientName)));
String sql = "r";
}
}
}
无论什么,我的代码不通过 int idClient = Integer.parseInt(String.valueOf(client_name_id_map.get(selectedClientName)));
code> NumberFormatException 。惊人的是,如果我删除 AutoCompleteDecorator
,那么一切都很好。
No matter what, my code do not pass int idClient = Integer.parseInt(String.valueOf(client_name_id_map.get(selectedClientName)));
it always ended up with NumberFormatException
. The amazing thing is, if I remove AutoCompleteDecorator
then everything works fine.
任何人知道如何解决这个问题?
Anyone know how to fix this please?
推荐答案
当您在地图中找不到要寻找的键时会出现问题。
The problem would occur when the key you are looking for is not found in the map.
在这种情况下:
-
client_name_id_map.get(selectedClientName)
将返回null -
String.valueOf(client_name_id_map.get(selectedClientName))
- 和
Integer.parseInt(null)
会抛出异常
client_name_id_map.get(selectedClientName)
would return nullString.valueOf(client_name_id_map.get(selectedClientName))
would return "null"- and
Integer.parseInt("null")
would throw an exception
一个简单的解决方案:
if(!selectedClientName.equals("Select Client"))
{
Integer idClient = client_name_id_map.get(selectedClientName);
if (idClient != null) {
// do something
}
String sql = "r";
}
这篇关于AutoCompleteDecorator正在中断ItemListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!