• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField & Keylistener  RSS feed

 
Roberto Frosi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ciao,
problem:
I added the KeyListener to my JTextField and when insert a text and I confirm, it triggers an event programmed by me. So everything is right. But if I do the setText ("Hello"); course it breaks loose keyPressed event



how do I invoke the KeyEvent with the setText?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert, and welcome to the Ranch!

For the purpose you describe, you should be using a DocumentListener or a Document Filter -- not a KeyListener.
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also don't listen for the Enter key. Instead you should add an ActionListener to the text field.
 
Roberto Frosi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you an example?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across this tutorial? Various sections of it are linked from tha API documentation of just about any Swing class.
Creating a GUI With JFC/Swing

There's a ton of example code there.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!