Solved

TextListener question

Posted on 2004-08-05
7
744 Views
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???
0
Comment
Question by:prain
[X]
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
7 Comments
 

Accepted Solution

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

Expert Comment

by:Mayank S
ID: 11726659
0
 
LVL 37

Assisted Solution

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

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

Expert Comment

by:zzynx
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.
0
 
LVL 3

Expert Comment

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

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

   public MyFrame()
    {
          this.setBounds(10,10,400,400);
          contentPane = this.getContentPane();
         contentPane.setLayout(null);

          textField = new JTextField();

          textField.addKeyListener(this);
          textField.setBounds(10,10,200,30);

         contentPane.add(textField);
        this.setVisible(true);
   }

     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)
                  System.exit(0);
     }


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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

632 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