0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 10 '16 at 05:34
  • 1
    `listFunctionNames = new JList(reflectedValues); listFunctionNames.setBounds(280,140,350,140); panel.add(listFunctionNames);` 1) Don't create a new list on action performed, just change the model (or content of the model). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with .. – Andrew Thompson Mar 10 '16 at 05:36
  • .. layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 10 '16 at 05:36
  • `(e.getSource() == comboModuleName)` is not how strings are compared in Java – MadProgrammer Mar 10 '16 at 05:58

1 Answers1

0

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);  
S.B
  • 678
  • 1
  • 5
  • 11
  • Thanks a lot for your help. Your solution worked for me :) @andrew Thanks sure I'll take care of the suggestions you have given. – shubham pandey Mar 10 '16 at 06:11