Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus Events and Text Fields

Status
Not open for further replies.

godcomplex

Programmer
Apr 24, 2002
94
US
Hello All,

I am having a problem catching the focus gained event in a JTextField. I have extended JTextField so that I can enter masks into the JTextField (the class was written before the realease of 1.4). The problem is that when the field gains focus, the caret moves to the end of the JTextField. This is the case because the text field already contains characters for the mask. For instance, the text field for a ssn number would contain " - - " initially. I have implemented the FocusListener interface and overwritten the focusGained method to reset the caret poistion to my own desired position, but it seems that although my code is being executed, the event is later processed by the system and the caret is moved back to the end of the text field. I have noticed that this is also a problem with the JFormattedTextField.

Is there any way to stop focus events from being processed any further? The consume() method on FocusEvent is not accessable from the listener.

-gc
 
I am a little confused (it is probably me hehe) by your explaination of your focus problem. Could you post a compilable sample class that demonstrates this problem?

I have implemented several JTextFields that use a mask of one kind or another via documents. I have not experience the problem described. I may be able to help if I can see the code, or a sample class that reproduces this behavior.

BTW, do not "HAVE" override a JTextField to provide masks for a field. You can extend the PlainDocument class and implement your mask operations in there and then set this as your document to a JTextField. This has been availble since the introduction of swing, so should work with any JDK with Swing. (1.4 has added a few new features to this but not needed to do what you need)

You would override the JTextField if you want to add more functionality to the class over an above the Document. But still using the documents interannly.


Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top