public void actionPerformed(ActionEvent e) {
String[] arr = {tf1.getText(),tf2.getText(),"","","","","","","","","","","","","","","",""};
switch(cb1.getSelectedItem().toString()){
case "Hillsborough":
arr[2] = "1";
break;
case "Pinellas":
System.out.println("HIT 1");
arr[3] = "1";
break;
case "Pasco":
System.out.println("HIT 2");
arr[4] = "1";
break;
case "Hernando":
System.out.println("HIT 3");
arr[5] = "1";
break;
case "Polk":
System.out.println("HIT 3");
arr[6] = "1";
break;
case "Manatee":
System.out.println("HIT 4");
arr[7] = "1";
break;
case "Sarasota":
System.out.println("HIT 5");
arr[8] = "1";
break;
case "Other Florida":
System.out.println("HIT 6");
arr[9] = "1";
break;
case "Other State/Country":
System.out.println("HIT 7");
arr[10] = "1";
break;
default:
break;
}
switch(cb2.getSelectedItem().toString()){
case "Active Duty":
arr[11] = "1";
break;
case "Coalition Forces":
arr[12] = "1";
break;
case "Dependent":
arr[13] = "1";
break;
case "Guard/Reserve":
arr[14] = "1";
break;
case "Retired":
arr[15] = "1";
break;
case "Veteran":
arr[16] = "1";
break;
case "Civilian":
arr[17] = "1";
break;
default:
break;
}
reader.submit(2, arr);
for(int c = 0; c < arr.length;c++)
System.out.print(arr[c]);
}
When i click the confirm button it triggers this. It, depending on the selected result, will add a 1 to a specific spot in the array and the pass it on through another method. It has nothing to do with the second method, the results aren't being accessed at all. The first result in the combo box doesn't have a switch/case associated with it because those are the placeholders for the selction boxes. Any way to fix it?
最佳答案
And what return cbo.getSelectedItem().toString()?
使用列表而不是数组。列表是动态的,因此您可以在需要时添加元素。当您不知道数据长度时使用数组是不可读且不必要的。
List<String> data = new ArrayList<>(); data.add(cbo.getSelectedItem().toString());
您确定添加了字符串而不是对象吗?如果您添加了一个对象,当您调用
toString
方法时,您将获得该类和它的hashCode
。因此,如果您已将一个对象添加到组合中,则需要重写 toString 方法并返回一个字符串(标识该对象的内容)。@Override public String toString() { return "some identifier"; }
关于java - JComboBox 未返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011984/