Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2007

Tip: Looking for answers? Try searching our database.

How to set a format in JFormattedTextField?

Thread view: 
RC - 11 Dec 2007 20:19 GMT
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);

    }
}
hiwa - 13 Dec 2007 03:14 GMT
> I created a DoubleFormattedTextField class shows below and
> new DoubleFormattedTextField("#.###", 0.0, 2.5);
[quoted text clipped - 34 lines]
>
> }

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.