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?