[英]Jlist is not getting updated when an action performed by selecting an item in Combobox
我試圖通過在組合框中選擇一個值來執行操作,選擇之后,應根據所選值對Jlist進行更新。 但是列表僅是第一次獲取值,但在更改值時不會更新。 但是值即將到來,並且執行操作,因為我可以看到值正在consol中。我的代碼如下:
ArrayList< String> ModuleNames = GetModuleNames();
String[] ModuleNames1 = ModuleNames.toArray(new String[ModuleNames.size()]);
comboModuleName = new JComboBox(ModuleNames1);
comboModuleName.setEditable(true);
comboModuleName.setSelectedItem(null);
comboModuleName.setBounds(280,80,350,40);
panel.add(comboModuleName);
comboModuleName.addActionListener(new ActionListener() {
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
String currentSelectedValue = comboModuleName.getSelectedItem().toString();
System.out.println("selected value is "+currentSelectedValue);
try
{ //collecting values from a function and want to populate in the list,currentSelectedValue
//currentSelectedValue is the value selected in the combobox based on this value function //returns some values as a arraylist
ArrayList CurrentModuleFunctions = getFunctionAndParametereNames(currentSelectedValue);
Vector reflectedValues = new Vector();
for (int i = 0; i < CurrentModuleFunctions.size(); i++) {
reflectedValues.addElement(CurrentModuleFunctions.get(i));
}
if(e.getSource() == comboModuleName) {
listFunctionNames = new JList(reflectedValues);
listFunctionNames.setBounds(280,140,350,140);
panel.add(listFunctionNames);
}
}
catch (ClassNotFoundException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
我不確定為什么Jlist沒有得到更新,因為當我在組合框中選擇新值時可以獲取值。
與其在actionPerformed()方法內部創建JList,不如在其外部創建
listFunctionNames = new JList();
listFunctionNames.setBounds(280,140,350,140);
panel.add(listFunctionNames);
在actionPerformed()內部,只需設置值
listFunctionNames.setListData(reflectedValues);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.