views:

82

answers:

2

I have applied a Formatter to a JFormattedTextField using a FormatterFactory, when a user clicks into the text field I want to select the contents.

A focus listener does not work as expected because the formatter gets called, which eventually causes the value to be reset which ultimately de-selects the fields contents. I think what is happening is that after the value changes, the Caret moves to the rightmost position and this deselects the field.

Does anyone have any knowledge of how to get around this and select the fields contents correctly?

+1  A: 

Hi, which jdk are you using - any chance this is a bug in it?

Chris Kimpton
+2  A: 

Quick and dirty workaround is to use EventQueue.invokeLater from your focusListener.

 EventQueue.invokeLater(new Runnable(){
  public void run() { yourTextField.selectAll();}
});
Peter