Formatting a textfield with currency style

2

I'm using NetBeans, and I can not show the value formatted as currency, for example $ 1,200.00. When I type in the text field, the zeros do not appear. I have a form with two fields: one for name and one for salary. In both, I'm using text field to receive the data, and a button to display the data in a dialog box.

IhaveaCallEmployeeclass,asperthecodebelow:

publicclassFuncionario{publicStringnome;publicdoublesalario;}

Displaybuttoncode,asbelow:

privatevoidbtnExibirActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:Funcionariofuncionario1=newFuncionario();DecimalFormatdf=newDecimalFormat();try{//Passaoconteúdodigitadoparaasvariáveisfuncionario1.nome=txtNome.getText();funcionario1.salario=(Double.parseDouble(txtSalario.getText()));JOptionPane.showMessageDialog(null,"\n*** Resultado ***" + "\n" +
        "Nome : " + funcionario1.nome.toUpperCase() + "\n"+            
        "Salário. .......: " + df.format(funcionario1.salario));
        }
        catch(Exception erro)
        {
            JOptionPane.showMessageDialog(null, erro + "Verifique se Você deixou algum campo vazio !!!", "Erro na Entrada de Dados", JOptionPane.INFORMATION_MESSAGE);
        }
}

I would also like to know how to test if the fields are empty and the salary field is only numeric.

    
asked by anonymous 29.08.2016 / 01:55

1 answer

1

try
{
    funcionario.salario = Double.parseDouble(txtSalario.getText().toString());
} 
catch(NumberFormatException e)
{
     //msg de erro aqui 
}

  • Locale locale = new Locale("pt", "BR");
    NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
    System.out.println(currencyFormatter.format(funcionario.salario));
    

    there change in place of println uses setText.

    ps: code not tested, question answered by phone application

        
    29.08.2016 / 08:18