Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Handling Key Press Event in Java

                         

Introduction

In this section, you will learn about the handling key press event in java. Key Press is the event is generated when you press any key to the specific component. This event is performed by the KeyListener. When you press or release keys for the writing purpose then key events are fired by the KeyListener objects which generates the KeyEvent environment for the component.

In this program, you will see how operations are performed when you press the key to the specific component. Here, the KeyPress is a constructor of the main class. This constructor creates a frame and set text field to the panel. Then the panel and the label have been set to the frame in the program. When you enter the character in the text field through the keyboard then your entered data will be displayed in the label. There are various method have been used to do the required are given : 

getKeyChar():
This is the method of the KeyEvent class which determines the character that has been entered by you. This method returns the character associated the KeyEvent.

KeyAdapter
This is the class is used to receive the keyboard events. It creates the keyListener objects using the addKeyListener() method. The generated event is passed to every KeyListener objects that receives such types of events using the addKeyListener() method of the object.

KeyPressed():
This method has been used in the program which receives the generated event when you press any key to the object. Above method also sets the text of the source of the event to the label.

Here is the code of program:

import java.awt.*;
import java.awt.event.*;

public class KeyPress extends Frame{
  Label label;
  TextField txtField;
  public static void main(String[] args) {
    KeyPress k = new KeyPress();
  }

  public KeyPress(){
    super("Key Press Event Frame");
    Panel panel = new Panel();
    label = new Label();
    txtField = new TextField(20);
    txtField.addKeyListener(new MyKeyListener());
    add(label, BorderLayout.NORTH);
    panel.add(txtField, BorderLayout.CENTER);
    add(panel, BorderLayout.CENTER);
    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);
      }
    });
    setSize(400,400);
    setVisible(true);
  }

  public class MyKeyListener extends KeyAdapter{
    public void keyPressed(KeyEvent ke){
      char i = ke.getKeyChar();
      String str = Character.toString(i);
      label.setText(str);
    }
  }  
}

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

thanks for tutorial. Is they way to moving image in applet with the use of Up/Down keys

Posted by veera on Tuesday, 03.18.08 @ 10:17am | #53141

i like the java

Posted by senthil on Monday, 08.20.07 @ 19:30pm | #23795

Hey, thanks for your tutorial, it helped me a lot. But i was wondering if there is a way to detect a keypress while the aplication is on background.
For example im using notepad and my aplication detects the keys that are being pressed.

Posted by Pedro on Tuesday, 06.5.07 @ 15:55pm | #18274

Latest Searches:
Framework for Java Dat
Use JFrame in JSP code
draw Polyline
standered jstl jar to
how to retrieve data f
iport syntax in java
java random numbers
Sending mails using ja
Define JSP
POST METHOD
Display name of system
communication
PHP Database Related M
Java Pass Value
Get Array Size
SIMPLE JAVA SOURSE COD
SaveErrors
getter setter methods
The JDBC(tm) Universal
text area
Jigloo
decimal to binary conv
Scrum
insert data to text fi
standard deviation arr
connect to sql databas
insert content to text
calender class
swap arrays in java
Photoshop Text Effects
java excel
decimal to unicode
string Compareing
how to get system scre
java write file line b
Area and circumference
\n java
jboss connection pool
how to print arraylist
Display name of system
struts.jar download
login code example in
netbeans jsp
ensureCapacity of vect
tattoo efectes
compression of a file
rmi
count the number of di
create xls using POI
Java Count words,reve
java.sql.Date
Quartz Tutorial
Date in JSP
Java show Vowels in th
code for Write a small
struts language swit
tibco
tabindex
setvisible tab
Socket tcp
Display name of system
what are the syntax in
Sentence case
set cell value JTable
jQuery To Hide the Div
conversion of decimal
seam tutorial
codebar
Retrieving Data From d
disable Jframe
apache derby embedded
richfaces a4j taglibr
constructor
selectOneRadio
java string
à¹?à¸?à¸?à¹???à¹?à¸?à¸
loading combo values f
bidirectional
space char
new language insert an
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.