【发布时间】:2016-03-10 05:29:46
【问题描述】:
我正在尝试通过在 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 没有更新,因为当我在组合框中选择新值时可以获得值。
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
listFunctionNames = new JList(reflectedValues); listFunctionNames.setBounds(280,140,350,140); panel.add(listFunctionNames);1) 不要在执行的操作上创建新列表,只需更改模型(或模型的内容)。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 .. -
.. white space 的布局内边距和边框。
-
(e.getSource() == comboModuleName)不是 Java 中比较字符串的方式
标签: java swing jcombobox jlist