Java Swing Text Format buildIntegerField(int min, int max)

Here you can find the source of buildIntegerField(int min, int max)

Description

Builds a formatted text field with specified min and max

License

Open Source License

Parameter

Parameter Description
min minimum value
max maximum value

Return

JFormattedTextField

Declaration

public static JFormattedTextField buildIntegerField(int min, int max) 

Method Source Code


//package com.java2s;
/*/*from   w w w.  java  2  s  .  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.border.Border;

import javax.swing.text.NumberFormatter;

public class Main {
    /**
     * <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. checkBoundaryCondition(final int pos, final Position.Bias b0, final Position.Bias[] biasRet, final Element neighboringElement, final boolean isLTR, final boolean toWest, final int start, final int length, final int startParagraph, final int endParagraph, final boolean nextIsLTR)
  2. createFormattedDateField(DateFormat dfFormat, boolean bOverwriteMode)
  3. createFormatter(String s)
  4. createFormatterMask(final String mask)