How can I show a value in a jTextField when selecting a Jcheckbox at runtime?

Asked

Viewed 271 times

0

inserir a descrição da imagem aqui

private void btCalcularActionPerformed(java.awt.event.ActionEvent evt) {                                           
    double salario = 0, irrf = 0, inss = 0, valorTotal = 0;

    FolhaPagamento pagamento = new FolhaPagamento(Double.parseDouble(txtValorSalario.getText()));

    salario = pagamento.getSalario();

    if(cbIRRF.isSelected()){
        irrf = salario * 0.17;
        txtIRRF.setText(String.valueOf(irrf));
    }
    else txtIRRF.setText("");

    if(cbINSS.isSelected()){
        inss = salario * 0.05;
        txtINSS.setText(String.valueOf(inss));
    }
    else txtINSS.setText("");

    valorTotal = salario + irrf + inss;
    txtValorTotal.setText(String.valueOf(valorTotal));

}

The user enters any numerical value related to the employee’s salary and can click on the checkboxes (JCheckBox) relating to incidental contributions. If the IRRF box is checked, the value of the Withheld Income Tax should appear in the corresponding Source, that is, 17% of the salary value. Similarly, if the INSS box is checked, the corresponding INSS value should appear (5%).

  • You can add the code of what you tried to ask in the question?

  • @Articuno pus

  • But your question is how to capture the selection event on JCheckBox in order to do something ?

1 answer

0

Just add an event to your checkbox, an actionPerformed is ideal, see the example below.

    JFrame frame = new JFrame("teste");
    JPanel painel = new JPanel(new GridLayout(1, 2));
    JTextField field = new JTextField();
    JCheckBox checkBox = new JCheckBox("Me selecione");
    checkBox.addActionListener(((e) -> {
        if (checkBox.isSelected()) {
            field.setText("Voce clicou o Checkbox Parabens!");
        } else {
            field.setText("");
        }

    }));
    painel.add(checkBox);
    painel.add(field);
    frame.add(painel);
    frame.setSize(500, 50);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Browser other questions tagged

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