Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 584537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:29+00:00 2026-05-13T14:54:29+00:00

I’m using a NumberFormatter and JFormattedTextField , but the .getValue() doesn’t return the same

  • 0

I’m using a NumberFormatter and JFormattedTextField, but the .getValue() doesn’t return the same value as the user is seeing.

I think the input-string is parsed using NumberFormats parse-method, and I get the Numberformat from NumberFormat.getNumberInstance(); with the actual Locale. So I don’t think I easily can extend it and write my own parse-method?

In example, if the user types 1234.487
the getValue() will return:
1234.487 but the user will be displayed 1,234.49

Another example, using NumberFormat.getCurrencyInstance();. The user types 1234.487 and the getValue() will return 1234.487 but the user will be displayed $1,234.49

Instead, I want an ParseException be generated if the Formatter can’t format the value without rounding. Same thing if the user types 4.35b6, by default the formatter will display 4.35 and the value will be 4.35, but I want a ParseException, because the user typed in an invalid value.

Here is the code that I have tried with:

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
final JFormattedTextField ftf = new JFormattedTextField(nf);
ftf.setValue(new BigDecimal("1234.50"));

// Print the value from ftf
final JTextField txt = new JTextField(12);
txt.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
        txt.setText(ftf.getValue().toString());
    }
});

How to get the same value as the user is seeing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:54:29+00:00Added an answer on May 13, 2026 at 2:54 pm

    You should extend NumberFormatter rather than NumberFormat and override stringToValue so that it verifies that when the string is parsed you get back the original value:

    class StrictNumberFormatter extends javax.swing.text.NumberFormatter {
        @Override
        public Object stringToValue(String text) throws ParseException {
            Object parsedValue = super.stringToValue(text);
            String expectedText = super.valueToString(parsedValue);
            if (! super.stringToValue(expectedText).equals(parsedValue)) {
                throw new ParseException("Rounding occurred", 0);
            }
            return parsedValue;
        }
    
        public StrictNumberFormatter(NumberFormat nf) {
            super(nf);
        }
    }
    
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    JFormattedTextField.AbstractFormatter formatter = new StrictNumberFormatter(nf);
    final JFormattedTextField ftf = new JFormattedTextField(formatter);
    ftf.setValue(new BigDecimal("1234.50"));
    

    This formatter will reject the new text if the values before and after rounding do not match.

    Note: this compares the values, not the strings. It will tolerate changes like removing grouping characters ("$1,000" => "$1000") and trailing zeros ("$1.20" => "$1.2"). Basically if the NumberFormat returns the same value then it is acceptable. But any restrictions imposed by the NumberFormat still apply, e.g. you must not remove the currency symbol or insert leading spaces etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I figured out a nice way to do it. It… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer You don't pass parameters to your listener callbacks (without extending… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer Well. This is a trick I used to do with… May 14, 2026 at 7:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.