从Java的组合框中删除所有项目 [英] Removing all Items from a combo box in Java
本文介绍了从Java的组合框中删除所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从组合框中删除所有项目
I need to remove all items from the combo box
int itemCount = combo.getItemCount();
for(int i=0;i<itemCount;i++){
combo.removeItemAt(0);
}
此代码将删除除最后一个项目以外的所有项目.它给出了NullPointerException. 该如何解决?
This code will remove all items except the last one. It gives a NullPointerException. How to fix that?
推荐答案
问题中的代码通常可以正常工作.但是,它看起来像一个线程问题.另一个线程可能正在弄乱这些项目.
The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items.
但是,我建议您最好使用removeAllItems();
方法:
However, I sugeest you should better use the removeAllItems();
method:
combo.removeAllItems();
这篇关于从Java的组合框中删除所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文