Hi!
this is an example code how to use itemlistener. I hope it helps
import java.awt.*;
import java.awt.event.*;
public class ColorButtons extends Frame implements ItemListener
{
Checkbox blue, red, yellow, pink, gray;
public ColorButtons()
{
CheckboxGroup group = new CheckboxGroup();
blue = new Checkbox("blue", false, group);
red = new Checkbox("red", false, group);
yellow = new Checkbox("yellow", false, group);
pink = new Checkbox("pink", false, group);
gray = new Checkbox("gray", false, group);
blue.addItemListener(this);
red.addItemListener(this);
yellow.addItemListener(this);
pink.addItemListener(this);
gray.addItemListener(this);
setLayout(new FlowLayout());
add(blue);
add(red);
add(yellow);
add(pink);
add(gray);
setSize(300,175);
}
public void itemStateChanged(ItemEvent e)
{
Color color = null;
Checkbox checkbox=null;
if(e.getStateChange() == ItemEvent.SELECTED)
{
checkbox = (Checkbox)e.getSource();
if(blue.getState())
color = Color.blue;
else if(red.getState())
color = Color.red;
else if(yellow.getState())
color = Color.yellow;
else if(pink.getState())
color = Color.pink;
else if(gray.getState())
color = Color.gray;
}
else
{
System.out.println("unexpected checkbox sending events: " +
checkbox.getLabel());
color = color.black;
}
setBackground(color);
repaint();
}
public static void main(
String[] args)
{
ColorButtons cb = new ColorButtons();
//Finish setting up the frame, and show it.
cb.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// cb.pack();
cb.setVisible(true);
}
}
// visit
www.configure-all.com compuetr programming tutorials including
java