Java JSpinner replaceSpinnerValue(JSpinner spinner, double value)

Here you can find the source of replaceSpinnerValue(JSpinner spinner, double value)

Description

replace Spinner Value

License

BSD License

Declaration

public static void replaceSpinnerValue(JSpinner spinner, double value) 

Method Source Code

//package com.java2s;
// LICENSE:      This file is distributed under the BSD license.

import javax.swing.*;
import javax.swing.event.ChangeListener;

import javax.swing.text.JTextComponent;

public class Main {
    public static void replaceSpinnerValue(JSpinner spinner, double value) {
        ChangeListener[] listeners = detachChangeListeners(spinner);
        spinner.setValue(value);/* w ww  . j a  v a  2  s  .  co  m*/
        reattachChangeListeners(spinner, listeners);
    }

    public static ChangeListener[] detachChangeListeners(JSpinner spinner) {
        ChangeListener[] listeners = spinner.getChangeListeners();
        for (ChangeListener listener : listeners) {
            spinner.removeChangeListener(listener);
        }
        return listeners;
    }

    public static void setValue(JComponent component, String value) {
        if (component instanceof JTextComponent) {
            ((JTextComponent) component).setText(value);
        }
        if (component instanceof JComboBox) {
            ((JComboBox) component).setSelectedItem(value);
        }
        if (component instanceof JList) {
            ((JList) component).setSelectedValue(value, true);
        }
    }

    public static void setValue(JComponent component, Object value) {
        String valueText = value.toString();
        if (component instanceof JTextComponent) {
            ((JTextComponent) component).setText(valueText);
        }
        if (component instanceof JComboBox) {
            ((JComboBox) component).setSelectedItem(valueText);
        }
        if (component instanceof JList) {
            ((JList) component).setSelectedValue(valueText, true);
        }
    }

    public static void reattachChangeListeners(JSpinner spinner, ChangeListener[] listeners) {
        for (ChangeListener listener : listeners) {
            spinner.addChangeListener(listener);
        }
    }
}

Related

  1. getSpinnerInt(JSpinner aSpinner)
  2. getSpinnerIntValue(final JSpinner spinner)
  3. getSpinnerIntValue(JSpinner spin)
  4. installSpinnerBugWorkaround(final JSpinner spinner)
  5. reattachChangeListeners(JSpinner spinner, ChangeListener[] listeners)
  6. setList(JSpinner spinner, List values)
  7. setRightAlignment(JSpinner spinner)
  8. setSpinnerFloatValue(JSpinner sp, float f)
  9. setSpinnerValue(JSpinner spinner, Integer i)