aspose file tools
The moose likes Swing / AWT / SWT / JFace and the fly likes JTextField and KeyEvents ... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JTextField and KeyEvents ..." Watch "JTextField and KeyEvents ..." New topic
Author

JTextField and KeyEvents ...

Tom Diamond
Ranch Hand

Joined: May 10, 2001
Posts: 98
Hello,
I have a JTextField and I want to capture key events from it. OK, I've written a nice KeyAdapter class and added it to the text field. But it seems that the event is proccessed firstly in my class and then in the JTextField's keyEvent listener. What I am saying is that I want to use the getText() method in my custom keyTyped() but it returns the text in the field without the last char.
So is there a way to send the keyEvent to the text field before I proccess it so that the text that getText() returns will be updated?
Tnx,
Tom.
Joe Gilvary
Ranch Hand

Joined: May 11, 2001
Posts: 152
That sounds weird.
If you do not find some more elegant way to
handle it, how about appending the
KeyEvent.getKeyChar() to the String that you
can see?
HTH,
Joe
Tom Diamond
Ranch Hand

Joined: May 10, 2001
Posts: 98
Well, that is what I thought at first but the problem is that I have to take care of non-printable chars (i.e. Backspace - Delete) and also see if the user has selected some text. I wonder if there is a better way to do it ...
Tom.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Tom,
The cleanest approach would be to extend the JTextField and override the processKeyEvent method. This would make sure that the keyEvent was handled by the TextField before you do your thing. See the code below for an example.

Regards,
Manfred.
 
 
subject: JTextField and KeyEvents ...
 
Threads others viewed
JTextField text reload problem
Identify a component that has been added by iteration
Extended JTextField Problem
Paste inserts 2 copies of clipboard
How does inner class access outter class functions?
developer file tools