document text preverfication
I'm developing an application that allows the user to specify
the scale of the graphics view.
The underlying value is stored as a double. The user can change
the scale value by typing the value in a text field.
Currently, the text field document preverifies the input to make
sure that it is a double formatted number being entered. How
can preverification be done using the NumberFormat class?
[438 byte] By [
rkippena] at [2007-10-1 21:38:28]

NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are.
NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.
You can use the below to check a number
Number NumberFormat.parse(String source)
Parses text from the beginning of the given string to produce a number
Cheers,
I don't think you understand the question.
Suppose there is a text field that accepts only numbers.
This means the text field should only reject any keys typed that
can never be part of a number.
For example, in en_US style numbers, the user could type the
negative sign '-'. However, once the negative sign is present,
it cannot appear again unless it is part of an exponent.
I want a class like this, but supports all locales:
public abstract class NumberDocument extends PlainDocument {
/**
A return value of true indicates that the text parsed correctly, but
it may not represent valid text. For example, a text field accepting
any number will have to accept the minus sign (-) for negative numbers,
but the minus sign by itself does not represent a valid value.
*/
public abstract boolean validateText(String text);
/**
The insertString method builds the new string from the offset and string parameters and
calls the validateText method passing in the new string. If the validate text method
returns false, and beepOnInvalidInsert is true, then the Toolkit.getDefaultToolkit().beep()
method is called.
*/
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
String t = getText(0, getLength());
String newStr = t.substring(0, offs) + str + t.substring(offs);
if (validateText(newStr))
super.insertString(offs, str, a);
else if (beepOnInvalidInsert)
Toolkit.getDefaultToolkit().beep();
}
}