• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bug or Feature of JComboBox

 
vinaya kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
i have below a code snippet in which to combobox i have added action and item listener
(i hope we can add both listeners at the same time).
when i select a item,sometimes the action listener
function is executed more than once and item listener also the same thing
can any one point out why...
how to avoid this .. is there anything wrong
in the code...
vinaya
import javax.swing.*;
import java.awt.event.*;
public class ComboTest extends JPanel
{
JComboBox combo;
Object arr[]=
{
"New",
"aRr",
"aRr1",
};

public ComboTest()
{
combo=new JComboBox(arr);
combo.addActionListener(new ComboActionListener());
combo.addItemListener(new ComboItemListener());
add(combo);
}

class ComboItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("Called Item Listener"+combo.getSelectedItem());
}
}

class ComboActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Called Action Listener"+combo.getSelectedItem());
}
}
public static void main(String args[])
{
ComboTest com=new ComboTest();
JFrame test=new JFrame();
test.getContentPane().add(com);
test.show();

}

}
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've also faced the same problem... don't know what to do...
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Sun bug report (you do know where it is right):
This is not a bug. ItemEvent sources are supposed to deliver two events every time the selection changes. One of the events is a SELECTED event the other is a DESELECTED event (capitalized because those are the names of the actual variables in ItemEvent). The following code demonstrates that it's acting as expected.
http://developer.java.sun.com/developer/bugParade/bugs/4128297.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!