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
-
Unable to receive email in HTML format on BB Torch 9860 with BIS
I recently purchased BB Torch 9860 with OS 7 and configured by personal and work email on BIS. I am receiving emails only in text format. In some emails with images that have hyperlink, I just see a link and images not displayed. All the images comes
-
How to use SYSDATE as a default value of a bind variable in a query report?
Hi, I want to use SYSDATE as default value for a bind variable in Query based report. I don't see any way to do it, someone helps? Thanks a lot. Paulo.
-
How do I set up PayPal in iTunes 10?
Hi everyone. I've tried setting up PayPal as my form of billing on iTunes 10, but every time I do, it redirects me to a page where I must fill out a debit or credit card's credentials. Is it even possible to do, or am I just missing something? Thanks
-
IPad mini for writing word documents?
Can I use the iPad mini for writing word documents etc. Or even if I can use an app and export it for word etc. Is it already loaded app or do I have to buy it?
-
What can I do if I have forgotten all of my security question??!!
I have forgotten all of my security questions on my IPad account and can't access the money o recently put on, I have no credit card information and I only use ITunes vouchers which I can't use!!!