Here you can find the source of selectAllOnFocusGained()
public static void selectAllOnFocusGained()
//package com.java2s; //License from project: Open Source License import java.awt.KeyboardFocusManager; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Main { private static final String PERMANENT_FOCUS_OWNER_PROPERTY_KEY = "permanentFocusOwner"; public static void selectAllOnFocusGained() { PropertyChangeListener listener = new PropertyChangeListener() { public void propertyChange(final PropertyChangeEvent e) { final Object value = e.getNewValue(); if ((value instanceof JTextField) && !(value instanceof JPasswordField)) { SwingUtilities.invokeLater(new Runnable() { public void run() { ((JTextField) value).selectAll(); }/*from w w w .ja v a 2 s . c o m*/ }); } } }; KeyboardFocusManager.getCurrentKeyboardFocusManager() .addPropertyChangeListener(PERMANENT_FOCUS_OWNER_PROPERTY_KEY, listener); } }