Here you can find the source of buildIntegerField(int min, int max)
Builds a formatted text field with specified min and max
Parameter | Description |
---|---|
min | minimum value |
max | maximum value |
public static JFormattedTextField buildIntegerField(int min, int max)
//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; } }