Weird behavior of JFormattedTextField with NumberFormatter and DecimalFormat

44 views Asked by At

I'm new here (I've read a lot, nut never posted before).

I'm encountering a weird problem on a JFormattedTextField. I'm trying to limit the input to numbers ranging from 9999.9 to -9999.9, but I can't seem to find a working solution. I tried using a DecimalFormat and a NumberFormatter, but it doesn't work as expected. I looked all over the net, but I can't seem to find an explanation of what I'm seeing (maybe I'm not searching for the right thing?).

Here is a small code that shows the problem:

import java.awt.*;

import javax.swing.*;
import javax.swing.text.*;

import java.text.*;

public class testwindow extends JPanel
{
    private JFormattedTextField field1;
    private JFormattedTextField field2;

    public testwindow()
    {
        try
        {
            //format text input fields (only numbers, dots and minus; overwrite; don't allow invalid chars)
            DecimalFormat decimalFormat = new DecimalFormat("#0.0;-#0.0");
            decimalFormat.setNegativePrefix("-");
            decimalFormat.setMinimumIntegerDigits(1);
            decimalFormat.setMaximumIntegerDigits(4);
            decimalFormat.setMinimumFractionDigits(1);
            decimalFormat.setMaximumFractionDigits(1);
            NumberFormatter realFormatter = new NumberFormatter(decimalFormat);
            realFormatter.setOverwriteMode(true);
            realFormatter.setAllowsInvalid(false);
            
            field1 = new JFormattedTextField(realFormatter);
            field1.setValue(0F);
            field2 = new JFormattedTextField(decimalFormat);
            field2.setValue(0F);
            
            setPreferredSize (new Dimension (215, 60));
            setLayout(new GridLayout(2,1));

            add (field1);
            add (field2);
        }
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }


    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new testwindow());
        frame.pack();
        frame.setVisible (true);
    }
}

Basically, I type 123456, and I'd expect the result to be 1234.5. However:

  • field1, with NumberFormatter applied, shows 4050.6 immediately
  • field2 shows 1234560.0 while typing, and changes to 4560.0 after focus is moved

Also, when pressing minus on field1, the sign changes correctly between positive and negative. In field2, unless the minus is written as first character, it disappears.

Last but not least, typing 9 as last char on field1 multiple times just causes the number to increase of 0.1 each time, making all the behavior really puzzling.

I like much more the field1 behavior regarding the number of digits enforcement, but I wonder whether there is a correct/better way to obtain what I need, that is (as example) "1234.5" and the number not increasing by typing anything else as last char?

0

There are 0 answers