Action Event in Applet

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

public class ActionListenerDemo1 extends JApplet implements ActionListener {
	JPanel p1;
	JButton b1, b2;
	JTextField t1;

	@Override
	public void init() {
		p1 = new JPanel();
		b1 = new JButton("button1");
		b2 = new JButton("button2");
		t1 = new JTextField(20);
		getContentPane().add(p1);
		p1.add(b1);
		p1.add(b2);
		p1.add(t1);
		b1.addActionListener(this);
		b2.addActionListener(this);
		// this refer to the object of current class
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == b1) {
			JOptionPane.showConfirmDialog(null, "u clicked button one",
					"button1", 2);
		}
		if (evt.getSource() == b2) {

			String str = t1.getText();
			if (str.length() == 0)
				JOptionPane.showConfirmDialog(null,
						"text field can not left blank", "button2", 2);
			else
				JOptionPane.showConfirmDialog(null, str, "button2", 2);

		}
	}
}

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