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.setSelectedItem and actionPerformed metho

843806May 29 2008 — edited May 31 2008
Hi,


I have a question about setSelectedItem method of JComboBox.
actionPerformed method of my class is called by Java, when I call setSelectedItem method of JComboBox.

1. Is there a way to stop calling the actionPeformed method when I execute setSelectedItem method of JComboBox.

My second issue is when I call setSelectedItem method within actionPerformed method, actionPerfromed method is not called again.

2. Why doesn't Java call actionPeformed method again?
package giu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;


public class TestEvents extends JFrame implements ActionListener{
    
    public JComboBox comboBox = new JComboBox();
    
    public TestEvents() {      
        comboBox.addItem("Item 1");
        comboBox.addItem("Item 2");
        comboBox.addItem("Item 3");
        comboBox.addActionListener(this);
        add(comboBox);
        setSize(100,100);
        setVisible(true);   
    }
    
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("actionPerformed");
        //Why doesn't this call to setSelectedItem method fires an event
        //and call actionPerfromed metho
        comboBox.setSelectedItem("Item 3");
    }
    
    
    public static void main(String args[])
    {
        TestEvents testEvents = new TestEvents();
        testEvents.comboBox.setSelectedItem("Item 2");
        testEvents.comboBox.setSelectedItem("Item 1");        
    }
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2008
Added on May 29 2008
7 comments
2,130 views