Jlist is not getting updated when an action performed by selecting an item in Combobox

房东的猫 提交于 2019-12-13 05:25:15

问题


I am trying to perform action by selecting a value in Combobox and after selection, based of the value selected Jlist should be updated. But list is taking value only first time but its not getting updated while changing the values. However Values are coming and action is performed as I can see values are coming in consol.My code is as follows:

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();
    }
    }
});

I am not sure why Jlist is not getting updated as I can get the values when I am selecting new value in combobox.


回答1:


Instead of creating the JList inside actionPerformed() method,create it outside

    listFunctionNames = new JList();
    listFunctionNames.setBounds(280,140,350,140);
    panel.add(listFunctionNames);

and inside actionPerformed(),just set the values

listFunctionNames.setListData(reflectedValues);  


来源:https://stackoverflow.com/questions/35908329/jlist-is-not-getting-updated-when-an-action-performed-by-selecting-an-item-in-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!