Java JSlider buildIntegerFieldWithSlider(final JSlider matchingSlider)

Here you can find the source of buildIntegerFieldWithSlider(final JSlider matchingSlider)

Description

Builds a formatted text field with specified min and max, and attaches the slider to it via listeners.

License

Open Source License

Parameter

Parameter Description
matchingSlider a parameter

Return

JFormattedTextField

Declaration

public static JFormattedTextField buildIntegerFieldWithSlider(final JSlider matchingSlider) 

Method Source Code


//package com.java2s;
/*//from  ww w  . jav a 2s .  co  m
 * Copyright 2004 (C) Ross M. Lodge
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JSlider;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.NumberFormatter;

public class Main {
    /**
     *
     * <p>
     * Builds a formatted text field with specified min and max, and attaches
     * the slider to it via listeners. The text field gets it's min and max from
     * the slider.
     * </p>
     *
     * @param matchingSlider
     * @return JFormattedTextField
     */
    public static JFormattedTextField buildIntegerFieldWithSlider(final JSlider matchingSlider) {
        final JFormattedTextField returnValue = buildIntegerField(matchingSlider.getMinimum(),
                matchingSlider.getMaximum());
        returnValue.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("value".equals(evt.getPropertyName())) {
                    Number value = (Number) evt.getNewValue();
                    if (value != null) {
                        matchingSlider.setValue(value.intValue());
                    }
                }
            }
        });
        matchingSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                int value = source.getValue();
                if (!source.getValueIsAdjusting()) { //done adjusting
                    returnValue.setValue(Integer.valueOf(value)); //update ftf value
                } else { //value is adjusting; just set the text
                    returnValue.setText(String.valueOf(value));
                }
            }
        });
        return returnValue;
    }

    /**
     * <p>Builds a formatted text field with specified min and max</p>
     * 
     * @param min minimum value
     * @param max maximum value
     * @return JFormattedTextField
     */
    public static JFormattedTextField buildIntegerField(int min, int max) {
        java.text.NumberFormat numberFormat = java.text.NumberFormat.getIntegerInstance();
        NumberFormatter formatter = new NumberFormatter(numberFormat);
        formatter.setMinimum(Integer.valueOf(min));
        formatter.setMaximum(Integer.valueOf(max));
        final JFormattedTextField returnValue = new JFormattedTextField(formatter);
        returnValue.setColumns(3);
        returnValue.addPropertyChangeListener(new PropertyChangeListener() {

            Border m_originalBorder = returnValue.getBorder();

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName() != null && evt.getPropertyName().equals("editValid")) {
                    if (evt.getNewValue() != null && evt.getNewValue() instanceof Boolean) {
                        if (((Boolean) evt.getNewValue()).booleanValue()) {
                            returnValue.setBorder(m_originalBorder);
                        } else {
                            returnValue.setBorder(BorderFactory.createLineBorder(Color.red));
                        }
                    }
                }
            }
        });
        return returnValue;
    }
}

Related

  1. addVerticalMouseListener(final JPanel panel, final JSlider slider)
  2. adjustWithoutNotifyingListeners(JSlider slider, int value)
  3. ajustarValorDeslizador(JSlider sldActual, int valor)
  4. createJSlider(final BoundedRangeModel model)
  5. createLabelField(String preText, String postText, JSlider s)
  6. createSliderPanel(final JSlider slider, String label, int width)
  7. fixWinSliderUI(JSlider s)