Example usage for javax.swing JSpinner addFocusListener

List of usage examples for javax.swing JSpinner addFocusListener

Introduction

In this page you can find the example usage for javax.swing JSpinner addFocusListener.

Prototype

public synchronized void addFocusListener(FocusListener l) 

Source Link

Document

Adds the specified focus listener to receive focus events from this component when this component gains input focus.

Usage

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * /*from  w  w  w.  j a  v a  2  s. c o  m*/
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner//from www .  ja v  a2s . c  o  m
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}