我正在尝试通过选择Combobox中的值来执行操作,并且在选择之后,应根据所选值更新Jlist。但是列表仅在第一次获得价值,但在更改值时未获得更新。然而,值即将到来并且执行操作,因为我可以看到值正在进入consol.My代码如下:
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没有得到更新,因为当我在组合框中选择新值时我可以得到值。
答案 0 :(得分:0)
不是在actionPerformed()方法中创建JList,而是在
之外创建它 listFunctionNames = new JList();
listFunctionNames.setBounds(280,140,350,140);
panel.add(listFunctionNames);
并且在actionPerformed()内,只需设置值
listFunctionNames.setListData(reflectedValues);