Java JComponent Size createDoubleSpinner(final double value, final double rangeMinimum, final double rangeMaximum, final double stepSize, final double bigStepSize, final String formatPattern)

Here you can find the source of createDoubleSpinner(final double value, final double rangeMinimum, final double rangeMaximum, final double stepSize, final double bigStepSize, final String formatPattern)

Description

create Double Spinner

License

Open Source License

Declaration

public static JSpinner createDoubleSpinner(final double value, final double rangeMinimum,
            final double rangeMaximum, final double stepSize, final double bigStepSize,
            final String formatPattern) 

Method Source Code


//package com.java2s;
/*/*from ww w  .  j  av a2  s  . co m*/
 * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

import javax.swing.AbstractAction;
import javax.swing.ActionMap;

import javax.swing.InputMap;
import javax.swing.JComponent;

import javax.swing.JSpinner;
import javax.swing.KeyStroke;
import javax.swing.SpinnerNumberModel;

import java.awt.event.ActionEvent;

import java.text.DecimalFormat;

public class Main {
    public static JSpinner createDoubleSpinner(final double value, final double rangeMinimum,
            final double rangeMaximum, final double stepSize, final double bigStepSize,
            final String formatPattern) {

        final SpinnerNumberModel numberModel = new SpinnerNumberModel(value, rangeMinimum, rangeMaximum, stepSize);
        final JSpinner spinner = new JSpinner(numberModel);
        final JComponent editor = spinner.getEditor();
        if (editor instanceof JSpinner.NumberEditor) {
            JSpinner.NumberEditor numberEditor = (JSpinner.NumberEditor) editor;
            final DecimalFormat format = numberEditor.getFormat();
            format.applyPattern(formatPattern);
            numberEditor.getTextField().setColumns(8);
        }

        final Double sss = stepSize;
        final Double bss = bigStepSize;
        final String bigDec = "dec++";
        final String bigInc = "inc++";

        final InputMap inputMap = spinner.getInputMap();
        final ActionMap actionMap = spinner.getActionMap();

        // big increase with "PAGE_UP" key
        inputMap.put(KeyStroke.getKeyStroke("PAGE_UP"), bigInc);
        actionMap.put(bigInc, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                numberModel.setStepSize(bss);
                numberModel.setValue(numberModel.getNextValue());
                numberModel.setStepSize(sss);
            }
        });

        // big decrease with "PAGE_UP" key
        inputMap.put(KeyStroke.getKeyStroke("PAGE_DOWN"), bigDec);
        actionMap.put(bigDec, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                numberModel.setStepSize(bss);
                numberModel.setValue(numberModel.getPreviousValue());
                numberModel.setStepSize(sss);
            }
        });

        return spinner;
    }
}

Related

  1. addWithSize(Component c, int width, int height)
  2. adjustWindowToMinimumSize(final Window window)
  3. autoFitResize(final JLayeredPane pane, final Component... comps)
  4. changeSize(JComponent comp, float newSize)
  5. contentSize(final Component component)
  6. differsSize(AttributeSet style, AttributeSet base)
  7. doSetSize(final Component component, final int width, final int height)
  8. doSetSizeInEDT(final Component component, final int width, final int height)
  9. emphasizeToolTip()