How to set a format in JFormattedTextField?

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);

}
}
 
H

hiwa

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);

}

}

The commit behavior of your class is normal and proper. If you want a
custom action on an invalid input, try override AbstractFoamatter
methods. Sorry I don't have enough time to try them. Or, InputVerifier
should also be useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top