// SimpleIntField.java package util; import java.awt.*; import java.awt.event.*; public class SimpleIntField extends SimpleTextField { public SimpleIntField (String label) { super(label); } public SimpleIntField (String label, int initVal) { super(label, "" + initVal); } public int getValue() { try { return Integer.parseInt(getText()); } catch (NumberFormatException ex) { return 0; } } public void setValue (int num) { setText(Integer.toString(num)); } // override this if you do not want to think about // the underlying text public void actionPerformed (int value) {} protected void textValueChanged (String text) { if (text.length() >= 1) { super.textValueChanged(text); // if not a digit, pretend it was not typed if (! Character.isDigit(text.charAt(text.length() - 1))) { setText(text.substring(0, text.length() - 1)); } } } }