Home -> Workshops -> STA 2000 Java Workshop -> NumberField class

NumberField class

Copy all into Editor and save as NumberField.java:

import java.awt.*;

public class NumberField extends TextField
{	protected boolean hasError = false;

	public NumberField()
	{	super();  }
	public NumberField(String number)
	{	super(number); 	}
	public NumberField(double number)
	{	super(String.valueOf(number)); 	}
	public void setNumber(String number)
	{	setText(number); 	}
	public void setNumber(double number)
	{	setText(String.valueOf(number)); 	}
	public double getNumber()
	{	try
		{	hasError = false;
			return Double.valueOf(getText()).doubleValue();
		}
		catch(NumberFormatException ne)
		{	hasError = true;
			return 0.0;
		}
	}
}