Swing Textfield selectAll without invokeLater

By | 2015-10-19

Get rid of the evil invokeLater.
InvokeLater will pollute the UX design.

private class SelectAllOfFocus extends FocusAdapter {
 @Override
 public void focusGained(FocusEvent e) {
   if (! e.isTemporary()) {
     JFormattedTextField textField = (JFormattedTextField)e.getComponent();
     // This is needed to put the text field in edited mode, so that its processFocusEvent doesn't
     // do anything. Otherwise, it calls setValue, and the selection is lost.
     textField.setText(textField.getText());
     textField.selectAll();
   }
 }
}

ref.: http://stackoverflow.com/questions/1178312/how-to-select-all-text-in-a-jformattedtextfield-when-it-gets-focus

Leave a Reply

Your email address will not be published. Required fields are marked *