Validating a JTextField

Im trying to valide the text of a JTextfield whenever the user types the key. I know this should be simple enough, however I must be using the wrong listener as when a KeyEvent is fired, it doesnt include the value I just typed. For example, if I already had a "4" in the field and typed a 5, the KeyEvent would return just "4" on a jtextfield.getText()

What am I missing? Am I using the wrong listener? Thanks in advance

Code:

searchfield.addKeyListener(new KeyAdapter()

{

public void keyPressed(KeyEvent e)

{

// do something useing searchfield.getText()

}

});

[623 byte] By [FadetoBlaha] at [2007-10-2 7:35:26]
# 1

I believe the problem is the order the events occur. ie as soon as you hit a key it fires the KeyEvent and sometime later that char is appended to the textfield. Try using the methods in the KeyEvent class to get the value of the key that caused the event to fire instead of getting the text from the textfield.

floundera at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi Flounder,

Thanks for the reply. I did get the character that was typed and append it to the end of the current text in the field. However, I implemented a typeahead on the JTextField and keys such as delete and arrows seem to be causing problems...so I was hoping there was some way to just get the text after the character is input so I wouldnt have to deal with the special characters.

FadetoBlaha at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 3
U could try the following - DocumentListener myListener = ?;JTextField myArea = ?;myArea.getDocument().addDocumentListener(myListener);
Muyeena at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 4
I would reccomend looking into using one of these[url] http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/Document.html[/url]
walken16a at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 5

Swing related questions should be posted in the Swing forum.

A DocumentListener won't work, because the Document has already been updated by the time the Document event is fired and you can't update the Document from within the Document.

If you want to validate the text as it is typed then you should use a DocumentFilter, which is discussed in the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter]Text Component Features[/url].

camickra at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 6

Hi Everyone,

Thanks for the replies and my apologies for posting in the wrong forum. I have overcome some of the problem I was describing earlier but probably the biggest I am still running into is that I want the JTextField to only allow the following:

a-z

A-Z

0-9

Backspace key

I have done this, however Im not sure how to make it so just the backspace key works. For example, I wouldnt want the user to be able to use the delete key or the arrow keys. JUST backspace.

Thanks again for your help

FadetoBlaha at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 7

I may be a little late on this, but here's something:

Your listener will always get notified before the JTextField's one. So I believe you should simply postpone your event-handling by using EventQueue.invokeLater(anAppropriateRunnable);

, such as to let the TextField update itself in between.

da.futta at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 8

> For example, I wouldnt want the user to be able to use the delete key or the arrow keys. JUST backspace.

Thats a terrible requirement.

As a user I wouldn't use you application. If I want to delete the first character in the text field why can't I use the arrow keys and then the delete key. Your requirement would force me to use the backspace key to remove all characters and then to retype all the characters (except the first one) again. Thats a lot of work and I don't see any reason for this requirement.

Anyway, if you really want to do then then read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url] for information on removing KeyStrokes from the InputMap.

camickra at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 9

Thanks camickr...that looks like it should work.

So you know, I am implementing a type-ahead for a part number search. A JTable is updated as the user types...and all the type-aheads I have seen implemented seem to force the user to backspace their entry out of the type ahead. The part numbers are not very long so it shouldnt be too big a deal.

Again thanks for your help

FadetoBlaha at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 10

I also had that problem. The reason why it happens it's because the key event is invoked before the jTextField is actuallized. You can solve the problem by using another listener. If you use a caretListener, in the caretUpdate method of the listener you can retrieve the jTextField.getText() method and obtain that way all the set of characters that were written, as the caret position changes after the last character is added, so when that event is invoked all is as you wanted to be.

I hope that helps.

Ata logui駉.

escollidoa at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...
# 11
A day late and a dollar short. ;-)
uncle_alicea at 2007-7-16 21:16:57 > top of Java-index,Java Essentials,Java Programming...