Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to set a format in JFormattedTextField?

Reply
Thread Tools

How to set a format in JFormattedTextField?

 
 
RC
Guest
Posts: n/a
 
      12-11-2007

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

}
}
 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      12-13-2007
On Dec 12, 5:19 am, RC <raymond.c...@nospam.noaa.gov> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Money Format + Decimal Place Format shannon Java 1 02-02-2006 03:47 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
NTFS quick format and normal format Guan Foo Wah MCSE 2 05-08-2004 11:35 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57