-1

I wanted to dynamically changed the value of my list based on the index of JComboBox. I have JComboBox where I get the index and return it to reuse somewhere in my class.

View

public class Frame extends JFrame
{
JComboBox firstCombo;
public Frame()
{
    addComponents(getContentPane());
    setVisible(true);
    pack();
}

public void addComponents(Container pane)
{
    firstCombo = new JComboBox();
    firstCombo.addActionListener(listener);
    add(firstCombo);

    DefaultComboBoxModel cbModel = new DefaultComboBoxModel(setGender());
    firstCombo.setModel(cbModel);

    int i = 0;
    Model m = new Model(i);

    List list = m.getName();

    for(Object s : list)
    {
        System.out.println(s);
    }
}

ActionListener listener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == firstCombo)
        {
            int i = firstCombo.getSelectedIndex();
            Model model = new Model(i);
            model.setIndex(i);
        }
    }
};
}

Model

public class Model 
{
int a;

public Model(int a)
{
    this.a = a;
}

public static String[] setGender()
{
    return new String[] {"Male", "Female"};
}

public void setIndex(int i)
{
    this.a = i;
}

public int getIndex()
{
    return a;
}

public List getName()
{
    List list = new ArrayList();

    if(getIndex() == 0)
    {
        list.add("Male");
    }
    else if(getIndex() == 1)
    {
        list.add("Female");
    }
    return list;
}

}

public class Jcombo {


public static void main(String[] args) {
    Frame frame = new Frame();
}

}

But returning list (return list) remains unchanged when I called this method getName() in my View. Any reasons why?

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • 2
    Have you tried to do a [mcve] ? Pretty sure you will find the problem by doing this – AxelH Jan 09 '17 at 15:08
  • PS : [How to print out all the elements of a List in Java?](http://stackoverflow.com/questions/10168066/how-to-print-out-all-the-elements-of-a-list-in-java) – AxelH Jan 09 '17 at 15:11
  • @AxelH Still didn't tried. But seeing the flow in debugger gives me the right values. – Francisunoxx Jan 09 '17 at 15:14
  • @NicolasFilotto thanks for this. I properly retrieve my data I just having a problem in my List. – Francisunoxx Jan 09 '17 at 15:15
  • 1
    What did you not tried ? Your code ? Well, provide a MCVE because I only see a cursor on a callable statement and a returned list printed (but not his content). So right now, I can't help more, I can only do suggestion and I don't want to ;) – AxelH Jan 09 '17 at 15:16
  • If you see it correctly in the debugger, not in real life, it hints at a concurrency issue. Please create a [mcve]. – RealSkeptic Jan 09 '17 at 15:16
  • Voting to close for no [mcve] – Hovercraft Full Of Eels Jan 09 '17 at 15:16
  • `List list = list = new ArrayList();` <- Is that a typo? Doesn't seem right.. – OH GOD SPIDERS Jan 09 '17 at 15:23
  • It is likely one of the two problems. 1) Concurrency problem; the deptId not visible by the other thread that updates the List (making the field volatile should fix that). 2) You never call the method getAllPositionId() to update the correct combobox. (Double check in a debugger if it is called and in what instance of the combobox the data is inserted) – k5_ Jan 09 '17 at 19:28
  • @AxelH I just updated my post please see. Thanks bro. – Francisunoxx Jan 11 '17 at 12:21
  • That is not minimal but this seems runnable. That's better. PS : I am not your "bro", stop that ;) – AxelH Jan 11 '17 at 12:42
  • @AxelH Ooops sorry. – Francisunoxx Jan 11 '17 at 12:43

1 Answers1

1
ActionListener listener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == firstCombo)
        {
            int i = firstCombo.getSelectedIndex();
            Model model = new Model(i);
            model.setIndex(i);
        }
    }
};

You are recreating a new instance of Model on each action, updating the index then nothing, so model will be lose and the garbage collector will take care of it. Keep the model instance to be reused later.

Note : getName return a List but in it, you always create a new instance and adding a single value. Not sure why

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • I just found a solution. I created a reference of List that being returned from Model class under the ActionListener `List list = model.getName();`. – Francisunoxx Jan 11 '17 at 13:04
  • @Francisunoxx You need to remember the scope of the instance you build. This is the problem here – AxelH Jan 11 '17 at 13:06
  • Yes I just removed the instance that I created in `addComponents()` method. What I did I just reused the `model` instance as what you said :) – Francisunoxx Jan 11 '17 at 13:07
  • @Francis that's a solution, I would suggest to follow some tutorial to see how to work with MVC to separated the View and the Model (the data, not your class name ;) ). This would be easier for you to have a good structure. – AxelH Jan 11 '17 at 13:12
  • I got confuse in that part. Gonna follow your tips. I appreciated your help :) – Francisunoxx Jan 11 '17 at 13:31