Creating field with weight format with two decimal places and 3 houses before the comma

0

I'm trying to create a "weight" field, but I'm having some problems.

Weight can range from 2 to 3 decimal places before the point. For example, it could be 80.00 kg or 100.00 kg. However, in the way I'm doing, it forces me to put the 3 houses before the point.

I also tried putting a setText("00.00"); to the field not "start" just with the point, plus it did not catch. How can I resolve this?

public class Peso extends JFrame {

    public static void main(String[] args) {
        Peso t = new Peso();
        t.setVisible(true);
    }

    CampoPeso peso = new CampoPeso();
    JPanel painel = new JPanel();

    public Peso() {
        JLabel label = new JLabel("Peso:");
        painel.add(label);
        painel.add(peso);
        add(painel);

        setSize(220, 100);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class CampoPeso extends JFormattedTextField {

    public CampoPeso() {
        setColumns(5);
        setText("00.00");
        try {
            MaskFormatter mf = new MaskFormatter("###.##");
            mf.install(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //para pegar o valor
    public Float getValor() {
        return new Float(getText().replace(".", "").replace(",", "."));
    }

    //setar o valor
    public void setValor(Object valor) {
        setText(valor.toString());
    }
}
    
asked by anonymous 31.07.2017 / 01:19

1 answer

3

According to solution in SOEn , you can customize the mask as follows:

public CampoPeso() {
    setColumns(5);

    setFormatterFactory(new AbstractFormatterFactory() {

        @Override
        public AbstractFormatter getFormatter(JFormattedTextField tf) {
            DecimalFormat format = new DecimalFormat();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);
            format.setRoundingMode(RoundingMode.HALF_UP);
            NumberFormatter formatter = new NumberFormatter(format);
            formatter.setAllowsInvalid(false);
            formatter.setMinimum(0.00);
            formatter.setMaximum(999.99);
            return formatter;
        }

    });

}

Briefly explaining, I'm creating a decimal numeric mask, which limits to two fractional houses, values between 0,00 to 999,99 .

Running:

Bymakingasmallchange,asthis this other SOEn response , you can also force the "." (dot) as the default decimal place separator:

class CampoPeso extends JFormattedTextField {

    public CampoPeso() {
        setColumns(5);

        setFormatterFactory(new AbstractFormatterFactory() {

            @Override
            public AbstractFormatter getFormatter(JFormattedTextField tf) {
                DecimalFormat format = new DecimalFormat();
                format.setMinimumFractionDigits(2);
                format.setMaximumFractionDigits(2);
                format.setRoundingMode(RoundingMode.HALF_UP);

                DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(getLocale());
                otherSymbols.setDecimalSeparator('.');

                format.setDecimalFormatSymbols(otherSymbols);
                NumberFormatter formatter = new NumberFormatter(format);
                formatter.setAllowsInvalid(false);
                formatter.setMinimum(0.00);
                formatter.setMaximum(999.99);
                return formatter;
            }

        });

    }

    // para pegar o valor
    public Float getValor() {
        return Float.valueOf(getText());
    }

   (...)

}

Note that you need to change your getValor() method, since now that the number already comes with a dot as a separator, you do not need to do a comma replace every time.

    
31.07.2017 / 02:20