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

x
?
Solved

TextListener question

Posted on 2004-08-05
7
Medium Priority
?
745 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 100 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 100 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

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