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
Solved

TextListener question

Posted on 2004-08-05
7
741 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
hibernate jars 4 45
Java: The Public Class Main 4 30
Java class and jar 3 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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