import java.awt.*; import corejava.*; /* A subclass of TextField for entering doubles. Based on Core Java's IntTextField. */ public class DoubleTextField extends TextField { /** * Creates the text field * @param def a double default, * @param min the minimum double accepted * @param max the maximum double accepted * @param size the size of the text field */ public DoubleTextField (double def, double min, double 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 () { double value; try { value = Double.valueOf (getText ().trim ()).doubleValue (); if (value < low || value > high) throw new NumberFormatException (); } catch (NumberFormatException e) { requestFocus (); return false; } return true; } /** * Get the double data value * @return the double data value * @remark if invalid, returns the valid prefix (or 0 if none) */ public double getValue() { return Format.atof (getText ()); } private double low; /* min value allowed */ private double high; /* max value allowed */ }