The
listener interface for receiving item events.
public interface ItemListener extends EventListener { void itemStateChanged(ItemEvent e); }
void
itemStateChanged(ItemEvent e)
Invoked
when an item has been selected or deselected by the user. The code
written for this method performs the operations that need to occur
when an item is selected (or deselected).
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ItemListenerEx { public ItemListenerEx(){ JComboBox box = new JComboBox(); box.addItem("One"); box.addItem("Two"); box.addItem("Three"); box.addItem("Four"); box.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ System.out.println(e.getItem() + " " + e.getStateChange() ); } }); JFrame frame = new JFrame(); frame.getContentPane().add(box); frame.pack(); frame.setVisible(true); } public static void main(String [] args) { ItemListenerEx obj1 = new ItemListenerEx(); } }
Select
Some Item on Combo Box and observe the Command Prompt
Output
No comments:
Post a Comment