Key Event

import javax.swing.*;
import java.awt.event.*;

public class KeyListenerDemo extends JApplet implements KeyListener {
	JPanel p1;
	JButton b1;
	JTextField t1;
	String str = "";

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

		t1.addKeyListener(this);
	}

	@Override
	public void keyPressed(KeyEvent evt1) {
	}

	@Override
	public void keyReleased(KeyEvent evt1) {
		if (evt1.getSource() == t1) {
			int i = evt1.getKeyCode();
			if (i >= 48 && i <= 57) {
				str = t1.getText();
			} else {
				t1.setText(str);
				showStatus("not valid character");
			}
			showStatus("" + i);
		}
	}

	@Override
	public void keyTyped(KeyEvent 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