R
RC
I created a DoubleFormattedTextField class shows below and
new DoubleFormattedTextField("#.###", 0.0, 2.5);
I expect it commit when I enter between 0.0 and 2.5 everything else
less than or great than or not double such as "a string" should revert.
But it doesn't work that way. I entered -1.2, 3.5 or "hello world", they
all committed.
Can anyone out there tell me what I have missed?
Thank Q very much!
class DoubleFormattedTextField extends JFormattedTextField {
private static final long serialVersionUID = 1L;
private double minimum;
private double maximum;
public DoubleFormattedTextField(String format, double minimum, double
maximum) {
super(new DecimalFormat(format));
this.minimum = minimum;
this.maximum = maximum;
NumberFormat doubleFormat = DecimalFormat.getNumberInstance();
doubleFormat.setMinimumFractionDigits(3);
doubleFormat.setMinimumIntegerDigits(1);
NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
doubleFormatter.setFormat(doubleFormat);
doubleFormatter.setMinimum(new Double(this.minimum));
doubleFormatter.setMaximum(new Double(this.maximum));
setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
}
}
new DoubleFormattedTextField("#.###", 0.0, 2.5);
I expect it commit when I enter between 0.0 and 2.5 everything else
less than or great than or not double such as "a string" should revert.
But it doesn't work that way. I entered -1.2, 3.5 or "hello world", they
all committed.
Can anyone out there tell me what I have missed?
Thank Q very much!
class DoubleFormattedTextField extends JFormattedTextField {
private static final long serialVersionUID = 1L;
private double minimum;
private double maximum;
public DoubleFormattedTextField(String format, double minimum, double
maximum) {
super(new DecimalFormat(format));
this.minimum = minimum;
this.maximum = maximum;
NumberFormat doubleFormat = DecimalFormat.getNumberInstance();
doubleFormat.setMinimumFractionDigits(3);
doubleFormat.setMinimumIntegerDigits(1);
NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
doubleFormatter.setFormat(doubleFormat);
doubleFormatter.setMinimum(new Double(this.minimum));
doubleFormatter.setMaximum(new Double(this.maximum));
setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
}
}