/*
 * [TestJSpinner.java]
 *
 * Summary: demonstrate the use of javax.swing.JSpinner.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;

import static java.lang.System.*;

/**
 * demonstrate the use of javax.swing.JSpinner.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestJSpinner
    {
    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * fire up a JFrame on the Swing thread
             */
            public void run()
                {
                final JFrame jFrame = new JFrame();
                final Container contentPane = jFrame.getContentPane();
                final JSpinner sizeSpinner = new JSpinner();
                sizeSpinner.setBackground( Color.BLACK );
                sizeSpinner.setForeground( Color.YELLOW );
                sizeSpinner.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
                sizeSpinner.setEnabled( true );
                // how much space you want to freeze the spinner at.
                Dimension d = new Dimension( 40, 25 );
                sizeSpinner.setMinimumSize( d );
                sizeSpinner.setPreferredSize( d );
                sizeSpinner.setMaximumSize( d );
                final SpinnerNumberModel sizeSpinnerModel =
                        new SpinnerNumberModel( /* initial value */ 18,
                                /* min */ 8,
                                /* max */ 72,
                                /* step */ 1 );
                sizeSpinner.setModel( sizeSpinnerModel );
                sizeSpinner.addChangeListener( new ChangeListener()
                    {
                    /**
                     * Invoked when the JSpinner changes, even one notch in
                     * moving to another value
                     * @param e a ChangeEvent object
                     */
                    public void stateChanged( ChangeEvent e )
                        {
                        // do something when user changes the value.
                        float setting = sizeSpinnerModel.getNumber().floatValue();
                        out.println( setting );
                        }
                    } );
                contentPane.add( sizeSpinner );
                jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                jFrame.pack();
                jFrame.setVisible( true );
                }
            } );
        } // end main
    }