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
 
 
Search All Tutorials
  

 
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
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

How to use KeyListener 

                         

Introduction 

In this section, you will learn how to handle different key events on the Java Awt component by using the event handling in Java. When the given program is executed then you will see the several key events these are applied on the component like the key pressed, key release and so on.

All the key events are handled through the KeyListener Interface that has been implemented in the main class of the program.

Description of this program:

In this program, you will see how to show display the specific massage on the frame according to the event. When you press the key then the message "Key Pressed" will be seen on the frame and if when you stop pressing keys then the message "Key Released" will be seen.

This is possible by using the KeyListener interface. It will generate the KeyEvent and you can the check the exact event by using different method like the keyTyped(), keyPressed() and the keyReleased() method which are used for retrieving the generated specific event.

Here is the code of this program:

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

public class KeyListenerTester extends Frame implements KeyListener{  
  TextField t1;
  Label l1;
    public KeyListenerTester(String s )   {  
    super(s)
  Panel p =new Panel();
  l1 = new Label ("Key Listener!" ;
  p.add(l1);  
  add(p);
    addKeyListener this 
  setSize 200,100 );
  setVisible(true);
  addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
  System.exit(0);
     }
  });
  }  
    public void keyTyped KeyEvent e ){  
    l1.setText("Key Typed");
     }  
    public void keyPressed KeyEvent e){  
    l1.setText "Key Pressed" 
    }  
    public void keyReleased KeyEvent e ){  
    l1.setText"Key Released" 
    }  
    public static void main (String[]args ){  
    new KeyListenerTester "Key Listener Tester" 
     }  
  }  
  

Output of program:

Download this program:

                         

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

Current Comments

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

its working properly I have copy pasted it and
tried it

Posted by P.Subhash Rayudu on Monday, 06.4.07 @ 18:17pm | #18189

This program is not working properly. You have to add the Textbox and add the listener with the text.

Posted by Anup Kumar Mandal on Thursday, 05.17.07 @ 14:18pm | #16248

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.

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

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

Copyright © 2008. All rights reserved.