ItemEvent
Advertisements
Event Handling for RadioButton, List, Choice
For handling event for RadioButton, List, Choice, checkbox you need to use ItemEvent class and ItemListener interface.
ItemEvent class and ItemListener interface are associalted with radio button, checkbox list, choice. This logic can be written in itemStateChanged() whenever we want to perform operation while changed the option in those component.
GUI Component | Event class | Listener Interface | Method (abstract method) |
---|---|---|---|
RadioButton, Checkbox, List, Choice | ItemEvent | ItemListener | public void itemStateChanged(ItemEvent e) |
Example of ItemEvent
import java.awt.*; import java.awt.event.*; class A { Frame f; Label statusLabel; A() { f=new Frame(); f.setSize(500,300); f.setLayout(new BorderLayout()); Panel p=new Panel(); p.setBackground(Color.cyan); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); Checkbox chkJava = new Checkbox("Java"); Checkbox chkHtml = new Checkbox("Html"); Checkbox chkPHP = new Checkbox("PHP"); p.add(chkJava); p.add(chkHtml); p.add(chkPHP); f.add("North",p); chkJava.addItemListener(new CustomItemListener()); chkHtml.addItemListener(new CustomItemListener()); chkPHP.addItemListener(new CustomItemListener()); f.add(statusLabel); f.setVisible(true); } class CustomItemListener implements ItemListener { public void itemStateChanged(ItemEvent e) { statusLabel.setText(e.getItem() +" Checkbox: " + (e.getStateChange()==1?"checked":"unchecked")); } } } class ItemEventEx { public static void main(String[] args) { A obj=new A(); } }
Download code Click
Google Advertisment