/* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-565755-5 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * A text field that accepts only integers * @version 1.01 15 Feb 1996 * @author Cay Horstmann */ package corejava; import java.awt.*; public class IntTextField extends TextField { /** * Creates the text field * @param def an integer default, * @param min the minimum int accepted * @param max the maximum int accepted * @param size the size of the text field */ public IntTextField(int def, int min, int max, int size) { super("" + def, size); low = min; high = max; } /** * Checks if the data entered is valid or not * @return true data ok, false otherwise * @remark if invalid, the field gets focus so the user can fix it */ public boolean isValid() { int value; try { value = Integer.valueOf(getText().trim()).intValue(); if (value < low || value > high) throw new NumberFormatException(); } catch (NumberFormatException e) { requestFocus(); return false; } return true; } /** * Get the integer data value * @return the integer data value * @remark if invalid, returns the valid prefix (or 0 if none) */ public int getValue() { return Format.atoi(getText()); } private int low; private int high; }