Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the same value as the user is seeing from a JFormattedTextField?
    primarykey
    data
    text
    <p>I'm using a <code>NumberFormatter</code> and <code>JFormattedTextField</code>, but the <code>.getValue()</code> doesn't return the same value as the user is seeing.</p> <p>I think the input-string is parsed using NumberFormats parse-method, and I get the Numberformat from <code>NumberFormat.getNumberInstance();</code> with the actual Locale. So I don't think I easily can extend it and write my own parse-method?</p> <p><b>In example</b>, if the user types <code>1234.487</code> the <code>getValue()</code> will return: <code>1234.487</code> but the user will be displayed <code>1,234.49</code></p> <p><b>Another example</b>, using <code>NumberFormat.getCurrencyInstance();</code>. The user types <code>1234.487</code> and the <code>getValue()</code> will return <code>1234.487</code> but the user will be displayed <code>$1,234.49</code></p> <p><b>Instead</b>, I want an ParseException be generated if the Formatter can't format the value without rounding. Same thing if the user types <code>4.35b6</code>, by default the formatter will display <code>4.35</code> and the value will be <code>4.35</code>, but I want a ParseException, because the user typed in an <b>invalid</b> value.</p> <p>Here is the code that I have tried with:</p> <pre><code>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()); } }); </code></pre> <p><b>How to get the same value as the user is seeing?</b></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload