Item Event

//<applet code=app5 height=500 width=600></applet>
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class ItemListenerDemo1 extends JApplet implements ItemListener {
	JPanel p1;
	JComboBox box;
	String str[] = { "red", "green", "blue", "black", "yellow", "cyan" };

	@Override
	public void init() {
		p1 = new JPanel();
		box = new JComboBox(str);
		getContentPane().add(p1);
		p1.add(box);
		box.addItemListener(this);
		// this refer to the object of current class
	}

	@Override
	public void itemStateChanged(ItemEvent evt) {
		int i = box.getSelectedIndex();
		if (i == 0)
			p1.setBackground(Color.red);
		if (i == 1)
			p1.setBackground(Color.green);
		if (i == 2)
			p1.setBackground(Color.blue);
		if (i == 3)
			p1.setBackground(Color.black);
		if (i == 4)
			p1.setBackground(Color.yellow);
		if (i == 5)
			p1.setBackground(Color.cyan);
	}
}

Download

Event handling in Java

Action Event with Menu Item
Action Event in Applet
Action Event
Adjustment Event
Focus Event
Item Event
Key Event
MOuse Event
Event with Timer
Window Event


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code