I have a Renderer class that extends JFormattedTextField

In its constructor I set up Formatter and FormatterFactory in order to pass the input format limitations, but still have ability to input characters of wrong format, not the restricted one

NumberFormat bigDecimalFormat = NumberFormat.getCurrencyInstance();
    bigDecimalFormat.setMaximumIntegerDigits(1);
    bigDecimalFormat.setMaximumFractionDigits(5);
    bigDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    NumberFormatter numberFormatter = new NumberFormatter(bigDecimalFormat);
    numberFormatter.setAllowsInvalid(false);
    numberFormatter.setValueClass(BigDecimal.class);
    numberFormatter.setMinimum(0.0);
    numberFormatter.setMaximum(1.0);
    //numberFormatter.setFormat(new DecimalFormat("#.####")); //tried to enable and disable it, but that doesn't help
    this.setFormatter(numberFormatter);
    this.setFormatterFactory(new DefaultFormatterFactory(numberFormatter));

and it is still possible to enter a text like 11.111.111, thet it gots exception on converting that to BigDecimal

In all explanations setting Formatter is enough, however, they use to do it through the constructor parameter, not the setter like me. What is the working way, if I extend the JFormattedTextField and need to set up the format in the constructor and want to limit the typed characters to the desired format only?

enter image description here

0

There are 0 best solutions below