Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 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();
                        }
                    });
                }
            }
        });
    }
}