通过在组合框中选择一个项目来执行操作时,Jlist不会更新 [英] Jlist is not getting updated when an action performed by selecting an item in Combobox

查看:70
本文介绍了通过在组合框中选择一个项目来执行操作时,Jlist不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在组合框"中选择一个值来执行操作,选择后,应基于所选值对Jlist进行更新.但是列表仅是第一次获取值,但在更改值时不会更新.但是值即将来临,并且执行操作,因为我可以看到值正在consol中.我的代码如下:

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

我不确定为什么Jlist没有得到更新,因为当我在组合框中选择新值时可以获取值.

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

推荐答案

与其在actionPerformed()方法内部创建JList,请在外部创建

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

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

并在actionPerformed()内部,只需设置值

and inside actionPerformed(),just set the values

listFunctionNames.setListData(reflectedValues);  

这篇关于通过在组合框中选择一个项目来执行操作时,Jlist不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆