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

0

privatevoidbtCalcularActionPerformed(java.awt.event.ActionEventevt){doublesalario=0,irrf=0,inss=0,valorTotal=0;FolhaPagamentopagamento=newFolhaPagamento(Double.parseDouble(txtValorSalario.getText()));salario=pagamento.getSalario();if(cbIRRF.isSelected()){irrf=salario*0.17;txtIRRF.setText(String.valueOf(irrf));}elsetxtIRRF.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 types a numeric value of the employee's salary and can click the checkboxes ( JCheckBox ) for the incident contributions. If the IRRF box is checked, the corresponding Withholding Income Tax value, that is, 17% of the salary value, should appear. In the same way, if the INSS box is checked, the corresponding INSS value (5%) should appear.

    
asked by anonymous 01.11.2017 / 18:56

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);
    
01.11.2017 / 21:15