Focus Listener

import javax.swing.*;
import java.awt.event.*;
public class FocusListenerDemo extends JApplet implements FocusListener {
	JPanel p1;
	JButton b1;
	JTextField t1, t2;

	@Override
	public void init() {
		p1 = new JPanel();
		t1 = new JTextField(20);
		t2 = new JTextField(20);
		b1 = new JButton("button1");
		getContentPane().add(p1);
		p1.add(t1);
		p1.add(t2);
		p1.add(b1);

		t2.addFocusListener(this);
	}

	@Override
	public void focusGained(FocusEvent evt1) {
		String str = t1.getText();
		if (str.length() < 6) {
			t1.setText("");
			JOptionPane.showConfirmDialog(null, "length can not be "
					+ "less than 6 characters", "error", 0);
			t1.requestFocus();
		}
	}

	@Override
	public void focusLost(FocusEvent evt1) {

	}
}

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