Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with caret position using JFormattedTextField w/ DefaultFormatter  RSS feed

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If focus is not already in my JFormattedTextField (only when using DefaultFormatter) when I click somewhere in the field to gain focus the caret position is always at the beginning of the field i.e. position 0. However if I do not use a DefaultFormatter then when I click in the field to gain focus the caret is where ever I clicked in that field.

Example not using DefaultFormatter


Example using DefaultFormatter


So as you can see from the comments in the second example it takes 2 button clicks to get the caret into the desired position, first on gains focus and second one moves the caret. However in the first example it only takes one button click because when focus is gained the caret is not defaulted to the first position it is actually put in the correct place.

Anyone know how I can fix the problem with the second example?? It is not a huge deal but I want all my text fields to act the same and I want to save the user useless clicks.

Thanks
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Brian Cole
Author
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a bug, it's a feature.

Seriously, there is code in DefaultFormatter that explicitly tries to do that. (It also has other annoyances, such as defaulting to overwrite mode when IMHO insert mode is usually more appropriate.) If you don't like it, then it's probably best not to use DefaultFormatter.

I'm curious why you want to use DefaultFormatter anyway. It doesn't really do anything. It's pretty much just there to be a superclass for NumberFormatter, DateFormatter, MaskFormatter, or a custom subclass.

---

I don't always agree with the implementation choices that were made with JFormattedTextField, but at least sometimes I do understand what motivated them. In this case, they were anticipating that String while editing can be different from the String while viewing. For example:

final JFormattedTextField intField = new JFormattedTextField( Integer.valueOf( 87654321 ) );

This is viewed with locale-appropriate delimiters, so for me (in the U.S.) is shows "87,654,321" until I click on the field and it drops the commas. Now if I were to use Mr. Camick's mouse adapter on this field and click between the 3 and the 2, the mouse adapter would set the caret between the 2 and the 1 because it doesn't know that the commas are being dropped.

That's why DefaultFormatter sets the caret instead to the start of the field. It would have been nice, though, if they had made it simple to turn off this behavior. (There are ways to disable it, as Mr. Camick demonstrates, but the obvious ways don't work. We can't override DefaultFormatter's positionCursorAtInitialLocation() method, for example, because it is package private.)

For something like
JFormattedTextField urlField = new JFormattedTextField( new java.net.URL("http://foo.com/bar") );
where the editing/viewing String representation is the same it would be handy. (Mr. Camick's mouse adapter does work in this situation.)
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is viewed with locale-appropriate delimiters, so for me (in the U.S.) is shows "87,654,321" until I click on the field and it drops the commas. Now if I were to use Mr. Camick's mouse adapter on this field and click between the 3 and the 2, the mouse adapter would set the caret between the 2 and the 1 because it doesn't know that the commas are being dropped.


I think I've found a solution that will handle this and the original problem.

You can check out Formatted Text Field Tips.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!