Format a coin-style textfield

Asked

Viewed 1,738 times

2

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

inserir a descrição da imagem aqui

I have a class called Employee, as code below:

public class Funcionario {
public String nome;
public double salario;
}

Button code Display as below:

   private void btnExibirActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    Funcionario funcionario1 = new Funcionario(); 
    DecimalFormat df = new DecimalFormat();
    try
    {

        // Passa o conteúdo digitado para as variáveis
        funcionario1.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 do it to test if the fields are empty and if the salary field is only numerical.

  • Do you want it to format as it is typed? If it is, it is not that way, but using mask in a Jformattedtextfield field.

  • I remember an answer from an old question about this, masks for Jformatted here, Help yourself, @Montesuma.

1 answer

1

tries this for numbers, when you parse if the string is not a valid numeric format parse throw a Numberformatexception

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

already for monetary values Java has a class to do this job would be Numberformat.class

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

there exchange in place of println uses setText.

ps: code has not been tested, question answered by phone app

  • And how to apply the double in the code below? It would be interesting to demonstrate the connection of the two passages, which were loose among themselves.

  • I did by phone , and only change the amount by variable functio.salario , I will edit the answer for you understand

  • Okay, I got it, thank you very much. I would like to know how to do using a formatted jFormattedTextField1 coin type field, using this symbol #,##0.00, when you type some value within the field, it returns me an error message: java.lang.Numberformatexception: Empty String. In this example I am using a jTextField1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.