Java JSpinner installSpinnerBugWorkaround(final JSpinner spinner)

Here you can find the source of installSpinnerBugWorkaround(final JSpinner spinner)

Description

Installs a workaround for bug #4699955 in a JSpinner.

License

Open Source License

Parameter

Parameter Description
spinner The spinner to fix

Declaration

public static void installSpinnerBugWorkaround(final JSpinner spinner) 

Method Source Code

//package com.java2s;
/*//from ww w .  ja  va  2s  .  c o m
 * Copyright (C) 2010-2012 Klaus Reimer <k@ailis.de>
 * See LICENSE.TXT for licensing information.
 */

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;

import javax.swing.SwingUtilities;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Installs a workaround for bug #4699955 in a JSpinner.
     * 
     * @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();
                        }
                    });
                }
            }
        });
    }
}

Related

  1. getSpinnerFloatValue(JSpinner sp)
  2. getSpinnerFormatter(JSpinner spinner)
  3. getSpinnerInt(JSpinner aSpinner)
  4. getSpinnerIntValue(final JSpinner spinner)
  5. getSpinnerIntValue(JSpinner spin)
  6. reattachChangeListeners(JSpinner spinner, ChangeListener[] listeners)
  7. replaceSpinnerValue(JSpinner spinner, double value)
  8. setList(JSpinner spinner, List values)
  9. setRightAlignment(JSpinner spinner)