Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


TextListener question

Posted on 2004-08-05
Medium Priority
Last Modified: 2009-12-16
I am confused how to use the TextListener on a JTextField.

Since there is no JTextListener Interface, how can I trap every key stroke on a JTextField
by  using the TextListener?

Does any one have an example of how to do this (on a JTextField and not TextField ) or otherwise???
Question by:prain
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

ikerasd earned 100 total points
ID: 11726647
you can use the KeyListener class and the methods keyPressed(KeyEvent e) and keyTyped(KeyEvent e)
LVL 30

Expert Comment

by:Mayank S
ID: 11726659
LVL 37

Assisted Solution

zzynx earned 100 total points
ID: 11726678
As an alternative you can add a document listener to the JTextField's document:

yourTextField.getDocument().addDocumentListener( someDocListener );
LVL 37

Expert Comment

ID: 11738234
>> how can I trap every key stroke on a JTextField
Just to know, why do you want that?
Is it to ignore/skip some characters typed in?
Then you should certainly use the document approach.

Expert Comment

ID: 11741511
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class MyFrame extends JFrame implements KeyListener
    JTextField textField;
     Container contentPane;

   public MyFrame()
          contentPane = this.getContentPane();

          textField = new JTextField();



     public void keyPressed(KeyEvent event)
System.out.println("PRESSED : " + event.getKeyChar());

     public void keyReleased(KeyEvent event)
System.out.println("RELEASED : " + event.getKeyChar());

      public void keyTyped(KeyEvent event)
System.out.println("TYPED : " + event.getKeyChar());

     public void processWindowEvent(WindowEvent event)
             if ( event.getID() == WindowEvent.WINDOW_CLOSING)

         public static void main(String args[])
                     MyFrame frame = new MyFrame();

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question