Java Swing Focus selectAllOnFocusGained()

Here you can find the source of selectAllOnFocusGained()

Description

select All On Focus Gained

License

Open Source License

Declaration

public static void selectAllOnFocusGained() 

Method Source Code


//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);
    }
}

Related

  1. requestFocus(final JComponent comp)
  2. requestFocus(final JComponent comp)
  3. requestFocus(final JComponent component)
  4. requestFocus(final Window win, final Component comp)
  5. requestFocusInWindow(final Component c)
  6. selectAndFocus(JComponent component)
  7. setFocus(JComponent component)
  8. setFocusableRecursively(final JComponent component, final boolean focusable, final boolean childsOnly)
  9. setFocusOn(Component component)