Key Event in Java with Examples
Table of Contents
a) Introduction to Key Event
b) Different types of methods of Key Event
c) Example with Source Code
d) Output
****************
Introduction
Key Event is generates if and only if the state of
the key is changed. It is generated by the components such as text field. The
event generated is passed through every KeyListener
or KeyAdapter
objects which is registered by addKeyListener to receive the
event. The interface KeyListener and class KeyAdapter
is found in java.awt.event package.
Key Event has three methods, they are;
a) public void keyPressed(KeyEvent e)
b) public void keyReleased(KeyEvent e)
c) public void keyTyped(KeyEvent e)
Let us see the example of Key Event using
KeyListener interface;
To use key listener interface we should implement it
to the main class as shown in the code.
Source Code:
/*
* To change this license
header, choose License Headers in Project Properties.
* To change this template
file, choose Tools | Templates
* and open the template
in the editor.
*/
package EventHandling;
/**
*
* @author AnkitPC
*/
import javax.swing.*; //for components
import java.awt.*; //for layout
import java.awt.event.*; //for event handling
public class KeyEventDemo implements KeyListener{
JFrame f1;
JLabel l1, l2,l3;
JTextField t1, t2;
KeyEventDemo(){
//constructor
f1= new JFrame();
f1.setSize(300,
300);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLayout(new
FlowLayout());
//creating a
object
l1= new
JLabel("Name");
l2= new
JLabel("Contact");
t1= new
JTextField(20);
t2= new JTextField(20);
l3=new
JLabel("Status");
//adding the
components to the frame
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
f1.add(l3);
//registering the event
t1.addKeyListener(this);
t2.addKeyListener(this);
f1.setVisible(true);
}
//main method
public static void
main(String[] args) {
//calling the
constructor
new
KeyEventDemo();
}
public void
keyPressed(KeyEvent e){
l3.setText("Key Pressed");
}
public void
keyReleased(KeyEvent e){
l3.setText("Key Released");
}
public void
keyTyped(KeyEvent e){
char ch=
e.getKeyChar();
if(e.getSource()==t1){
if(!(ch>='a'
&& ch<='z' || ch>='A' && ch<='Z')){
e.consume();
}
}
else
if(e.getSource()==t2){
String s=
t2.getText();
int len=
s.length();
if(len>=10){
e.consume();
}
if(!(ch>='0' && ch<='9')){
e.consume();
}
}
}
}
Output:
The first image is the initial output if you run the
program.
If you press any key in the name field then it will
only take the a-z character but not numbers. The screen shot of the key pressed
is done by pressing the shift key.
In the contact field, you can only type the numbers not characters. As shown in the figure.
Now let us see the example of the key event using
key adapter;
To use the adapter class, we should extends to the
main class as shown in code below.
Source Code:
/*
* To change this license
header, choose License Headers in Project Properties.
* To change this template
file, choose Tools | Templates
* and open the template
in the editor.
*/
package EventHandling;
/**
*
* @author AnkitPC
*/
import javax.swing.*; //for components
import java.awt.*; //for layout
import java.awt.event.*; //for event handling
public class KeyEventDemo extends KeyAdapter{
JFrame f1;
JLabel l1, l2;
JTextField t1, t2;
KeyEventDemo(){
//constructor
f1= new JFrame();
f1.setSize(300,
300);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLayout(new
FlowLayout());
//creating a
object
l1= new
JLabel("Name");
l2= new
JLabel("Contact");
t1= new
JTextField(20);
t2= new
JTextField(20);
//adding the
components to the frame
f1.add(l1);
f1.add(t1);
f1.add(l2);
f1.add(t2);
//registering the event
t1.addKeyListener(this);
t2.addKeyListener(this);
f1.setVisible(true);
}
//main method
public static void
main(String[] args) {
//calling the
constructor
new
KeyEventDemo();
}
public void
keyTyped(KeyEvent e){
char ch=
e.getKeyChar();
if(e.getSource()==t1){
if(!(ch>='a' && ch<='z' || ch>='A' &&
ch<='Z')){
e.consume();
}
}
else
if(e.getSource()==t2){
String s=
t2.getText();
int len=
s.length();
if(len>=10){
e.consume();
}
if(!(ch>='0' && ch<='9')){
e.consume();
}
}
}
}
Output:
The first image is the initial output of the program.
In this program I have removed the JLabel l3 and methods keyPressed() and
keyReleased().
The name field only contains a-Z characters only and contact field only contains numbers. You can check it by using the source code above.
*********
0 Comments