Java 向JComboBox提供数据的正确方式是什么?
向Java 向JComboBox提供数据的正确方式是什么?,java,swing,Java,Swing,向JComboBox馈送数据的正确方式是什么?我试图将一个字符串数组馈送到之前启动的JComboBox,我得到了一个NullPointerException 代码: 编辑:我这里的问题是每次我想使用时都需要更新JComboBox中的数据,因为数组中的字符串可能与我第一次使用组合框时不同 每次我想使用时都需要更新JComboBox中的数据,因为数组中的字符串可能与我第一次使用组合框时不同 尽管有时可以使用setModel()替换ComboBoxModel,如图所示,但您可能希望使用后跟调用的循环来
JComboBox
馈送数据的正确方式是什么?我试图将一个字符串
数组馈送到之前启动的JComboBox
,我得到了一个NullPointerException
代码:
编辑:我这里的问题是每次我想使用时都需要更新JComboBox
中的数据,因为数组中的字符串可能与我第一次使用组合框时不同
每次我想使用时都需要更新JComboBox
中的数据,因为数组中的字符串可能与我第一次使用组合框时不同
尽管有时可以使用
setModel()
替换ComboBoxModel
,如图所示,但您可能希望使用后跟调用的循环来更新模型。currentGames.get(currentGame).currentPlayers()的数据类型是什么?名单?地图。。。它是多线程的吗?确保没有比赛条件。例如,currentGames.get(currentGame).currentPlayers()
可能会返回在循环执行期间变为非实际的数字,如果有另一个线程向列表中添加/删除元素。首先使用静态数据检查GUI元素。currentGames.get(currentGame).currentPlayers()
返回一个int,而currentGames.get(currentGame).getPlayer(i).getId()
返回一个字符串。它不是多线程的。我这里的问题是,每当我想使用JcomboBox时,我都需要更新JcomboBox中的数据,因为数组中的字符串可能与我第一次使用ComboBox时不同。编译器给我一个异常,当行playerbox.setModel(model)调用code>投票以重新打开,因为NullPointerException
是nominal question.Thx附带的。它以你介绍的另一种方式工作
public void readPlayers(){
String[] arr = new String[currentGames.get(currentGame).currentPlayers()];
for(int i = 0; i <currentGames.get(currentGame).currentPlayers(); i++){
arr[i] = "Player " + (i + 1) + currentGames.get(currentGame).getPlayer(i).getId();
}
DefaultComboBoxModel model = new DefaultComboBoxModel(arr);
playersBox.setModel( model);
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException