Solved

TextListener question

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

Industry Leaders: 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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

761 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