删除 JCombo Box 中的所有项目,并且当组合框为空时不触发事件
Remove all Items in a JCombo Box and not trigger an event when the combo box is empty
我有一个 Jcombobox,它加载了数据库中的数据。
当用户执行某个操作时,我通过删除组合框中的所有项目并向其添加新的项目列表来重新加载组合框中的项目。
事情是这样的,我在组合框中添加了一个事件侦听器。即使组合框为空,也会触发事件。
我已经尝试通过所有这些来捕获 null:
if (categoryCBox.getModel().getSize() == 0){
//dont do anything with the combobox
}
if(categoryCBox.getSelectedIndex() == -1){
//dont do anything with the combobox
}
if(categoryCBox.getItemCount() == 0){
//dont do anything with the combobox
}
if (categoryCBox.getSelectedItem().toString().equals("")){
}
但是,由于删除了组合框中的所有项目,itemStateChangedEvent 仍然被触发,并为我提供了一个指向附加到它的方法的空指针。
如何避免空指针错误。
How can I avoid the null- pointer error.
- 从组合框中删除侦听器
- 重新加载项目
- 将侦听器添加回组合框
我有一个 Jcombobox,它加载了数据库中的数据。
当用户执行某个操作时,我通过删除组合框中的所有项目并向其添加新的项目列表来重新加载组合框中的项目。
事情是这样的,我在组合框中添加了一个事件侦听器。即使组合框为空,也会触发事件。
我已经尝试通过所有这些来捕获 null:
if (categoryCBox.getModel().getSize() == 0){
//dont do anything with the combobox
}
if(categoryCBox.getSelectedIndex() == -1){
//dont do anything with the combobox
}
if(categoryCBox.getItemCount() == 0){
//dont do anything with the combobox
}
if (categoryCBox.getSelectedItem().toString().equals("")){
}
但是,由于删除了组合框中的所有项目,itemStateChangedEvent 仍然被触发,并为我提供了一个指向附加到它的方法的空指针。 如何避免空指针错误。
How can I avoid the null- pointer error.
- 从组合框中删除侦听器
- 重新加载项目
- 将侦听器添加回组合框