1
我有這樣的代碼是從我的數據庫中讀取數據,並顯示在一個組合框:獲得所選項目
//this is inside a thread
@Override
public void readResponse(InputStream input) throws IOException {
InputStreamReader reader = new InputStreamReader(input);
JSONParser parser = new JSONParser();
Hashtable response = parser.parse(reader);
java.util.List allResult = (java.util.List) response.get("AllResult");
System.out.println(allResult);
try {
String[] data = new String[allResult.size()];
for (int i = 0; i < allResult.size(); i++) {
Object obj = allResult.get(i);
String result = (String) ((Hashtable) obj).get("Status");
String brokerName = (String) ((Hashtable) obj).get("brokerName");
if (result.equalsIgnoreCase("ok")) {
for (int j = 0; j < 4; j++) {
data[i] = brokerName;
}
}
}
ComboBox brokerNames = new ComboBox(data);
assignCon.addComponent(brokerNames);
} catch (Exception ex) {
ex.printStackTrace();
}
} //all these work well
我需要從組合框獲得所選擇的項目,當我點擊一個這樣的提交按鈕:
Button ass = new Button("ASSIGN");
ass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String names = brokerNames.getSelectedItem.toString();
}
});
眼下,它給我的錯誤是由於事實,我不能從線程之外訪問brokerNames。有人可以告訴我我做錯了什麼嗎?或者更好的方式去做這件事?謝謝。
好的,謝謝@Hovercraft,我已經這麼做了 - 在gui創作中加入了組合框,並將它作爲課堂上的一個領域。然後我在我的線程中使用了這行代碼:brokerNames.addItem(data) 但它給了我這個格式的名字:ljava.lang.string; @ 22e86b30 請問我做錯了什麼? – Grace
謝謝@Hovercraft,我解決了這個問題。 – Grace