Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JComboBox getSelectedItem not working

843806May 28 2007 — edited May 28 2007
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]
   
...
public void updateCombos(){
         try{
       Vector years = jBridge.vectorSelect("select distinct year " +
                                            "from movie.movie " +
                                            "order by year desc");
                        
                        yearCombo = new JComboBox(years);
                        yearCombo.addActionListener(this);
...

 public void actionPerformed(ActionEvent e) {
        	String year = (String)yearCombo.getSelectedItem();
        	int year1 = yearCombo.getSelectedIndex();
        	log.warn("*********year combo clicked: " + year + " " + year1);
...
            else if(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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2007
Added on May 28 2007
4 comments
862 views