The java.awt.event.ItemListener interface is responsible for processing events java.awt.event.ItemEvent action generated by the activation of checkboxes (Checkbox), option buttons (CheckboxGroup) and items in menus option type (CheckboxMenuItem). Its implementation requires the inclusion of the method itemStateChanged these classes.
Method | Description |
getItem() | Returns the itemthat triggered theevent. |
getStateChange() | Returns thetype ofstate changeoccurred. |
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleCheckBox extends JFrame implements ItemListener
{
FlowLayout Flw = new FlowLayout();
JLabel LblChc = new JLabel("What would you like to Choose?");
JCheckBox Cappuccino = new JCheckBox("Coffee", false);
JCheckBox SoftDrink = new JCheckBox("Pepsi", false);
JCheckBox Mlk = new JCheckBox("Milk", false);
JCheckBox Wtr = new JCheckBox("Water", false);
String Outpt,InsChsn;
public JavaExampleCheckBox()
{
super("Java Example Of Check Box Function");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
LblChc.setFont(new Font("Times New Roman", Font.ITALIC, 22));
Cappuccino.addItemListener(this);
SoftDrink.addItemListener(this);
Mlk.addItemListener(this);
Wtr.addItemListener(this);
add(LblChc);
add(Cappuccino);
add(SoftDrink);
add(Mlk);
add(Wtr);
}
public void itemStateChanged(ItemEvent Chk){}
public static void main(String[] aa)
{
final int FRAME_WIDTH = 330;
final int FRAME_HEIGHT = 110;
JavaExampleCheckBox Frm =new JavaExampleCheckBox();
Frm.setSize(FRAME_WIDTH, FRAME_HEIGHT);
Frm.setVisible(true);
}
}