JComboBox getSelectedItem not working

Hey guys. In my application I have a JComboBox called yearsCombo which has different years as elements. I've attached an actionListener to the combo. When I change the combobox, in the actionPerformed it is not recognizing that e.getsource == yearsCombo. Also, when I get the selectedItem of the combo, it always returns the first item, even when I select another item. Some of the relevent code is below. Any ideas as to why the actionEvent is not equals to the combo and why the get selecteditem is not working?

Here are the elements of the combo.

[All, 2006, 2005, 2004, 1999, 1998, 1990, 1984, 1983, 1976]

...

publicvoid updateCombos(){

try{

Vector years = jBridge.vectorSelect("select distinct year " +

"from movie.movie " +

"order by year desc");

yearCombo =new JComboBox(years);

yearCombo.addActionListener(this);

...

publicvoid actionPerformed(ActionEvent e){

String year = (String)yearCombo.getSelectedItem();

int year1 = yearCombo.getSelectedIndex();

log.warn("*********year combo clicked: " + year +" " + year1);

...

elseif(e.getSource() == yearCombo){

log.warn("YEARS CLICKED!");

}

...

}

output

[*WARN*] MovieOrganizer.-1: enter action performed: javax.swing.JComboBox[,267,5,56x25,layout=javax.swing.plaf.metal.Metal

ComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777544,maximumSize=,minimumSize=,prefe

rredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1998]

[*WARN*] MovieOrganizer.-1: *********year combo clicked: All 0

[2324 byte] By [tsylvesta] at [2007-11-27 5:43:26]
# 1
> When I change the combobox,Don't change the combo box. Change the ComboBoxModel using the setModel(...) method.
camickra at 2007-7-12 15:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 2
I'm not sure what you mean. I was talking about when I click on the combobox and change the selected value.
tsylvesta at 2007-7-12 15:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 3

It would seem to me that the action that is triggering the call to actionPerformed is not in fact coming from "yearCombo".

This works perfectly fine for me:

public class ComboDemo extends JPanel implements ActionListener

{

private JComboBox comboBox;

private JLabel label;

public ComboDemo() {

comboBox = new JComboBox(new String[] {"All", "2006", "2005", "etc"});

comboBox.addActionListener(this);

label = new JLabel("Please select a year");

add(comboBox);

add(label);

}

public void actionPerformed(ActionEvent e) {

JComboBox box = (JComboBox)e.getSource();

if (box != comboBox) {

System.err.println("Wrong box!");

return;

}

String s = (String)box.getSelectedItem();

label.setText(s);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

JFrame frame = new JFrame("JComboBox demo");

frame.add(new ComboDemo());

frame.pack();

frame.setVisible(true);

}

});

}

}

dwga at 2007-7-12 15:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I'm not sure what you mean. I was talking about when I click on the combobox and change the selected value.

Well, with a few random lines of code I can't really tell what you are doing so I made a guess.

First you say the combo box contains a list of years:

[All, 2006, 2005, 2004, 1999, 1998, 1990, 1984, 1983, 1976]

Then you post some code that shows the creation of a new combo box:

> yearCombo = new JComboBox(years);

Just because you create new combo box does not mean the combo box get displayed on the GUI. Its just a variable in memory that points to a combo box Object, but the GUI still references the original combo box.

The code in your ActionListener will reference this new combo box but the source or the ActionEvent will be the original combo box.

So if you want to update the items in a combo box you don't create a new combo box, you create a new model and update the combo box with this new model.

camickra at 2007-7-12 15:23:02 > top of Java-index,Desktop,Core GUI APIs...