KeyListerer

Hello:
I'd like add a KeyListener on a JTextField, but my coding has a error, I can not fix it, please help me. Thanks.
it complains as following when I run my coding.
D:\314project\test3\TestKeyListener.java:29: cannot resolve symbol
symbol : class KeyListener
location: class TestKeyListener
          jTF.addKeyListener (new KeyListener()
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class TestKeyListener extends JFrame{
     private static TestKeyListener tKL;
     private static JTextField jTF;
     private static Container c;
     public static void main(String [] args){
          tKL=new TestKeyListener();
          tKL.setVisible(true);
     public TestKeyListener(){
          setSize(400,400);
          setTitle("TestKeyListener");
          jTF=new JTextField();
          c=getContentPane();
          c.setLayout(new BorderLayout());
          addKL();
          setUp();
     private void addKL(){
          jTF.addKeyListener (new KeyListener()
public void keyPressed (KeyEvent keye)
// other codes goes here
// to print out the text in the textfield see code below
String printout = jTF.getText();
System.out.println (printout);
     //put the JTextField in the centre of the JFrame
     private void setUp(){
     c.add(jTF,BorderLayout.CENTER );

hi this is the solution to ur problem...KeyListener does not really solve what u want to use it for cos, keylistner sends to the command prompt as u type in ur application but i used ActionListener which is just what u want.
// This code works just fine.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestKeyListener extends JFrame implements ActionListener
private static TestKeyListener tKL;
private static JTextField jTF;
private static Container c;
public static void main(String [] args)
tKL=new TestKeyListener();
tKL.setVisible(true);
public TestKeyListener()
setSize(400,400);
setTitle("TestKeyListener");
jTF=new JTextField();
jTF.addActionListener (this); // note the adding of the listener to the textfield
c=getContentPane();
c.setLayout(new BorderLayout());
//addKL(); //note comment here also
setUp();
//*******************************commented out*************************
/* private void addKL(){
jTF.addKeyListener (new KeyListener()
public void keyPressed (KeyEvent keye)
// other codes goes here
// to print out the text in the textfield see code below
String printout = jTF.getText();
System.out.println (printout);
//*******************************commented out*************************
public void actionPerformed (ActionEvent evt)
     String printout = jTF.getText();
     System.out.println (printout);
//put the JTextField in the centre of the JFrame
private void setUp()
c.add(jTF,BorderLayout.CENTER );
but if you want the keylistener example let me know.
Regards

Similar Messages

Maybe you are looking for