Solved

TextListener question

Posted on 2004-08-05
7
738 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
Comment Utility
you can use the KeyListener class and the methods keyPressed(KeyEvent e) and keyTyped(KeyEvent e)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 25 total points
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 44
Java and GPO 11 45
eclipse formatting 6 38
Java / Linux and Regular Expressions 11 43
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now