0

What can I do to make the "sueldo, salud, AFP" textfields to accept only numbers?

    private void sueldoActionPerformed(java.awt.event.ActionEvent evt) {                                       

}                                      

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String sueldo = this.sueldo.getText();
    int sueldoInt = Integer.parseInt(sueldo);
    String salud = this.salud.getText();
    int saludInt = Integer.parseInt(salud);
    String afp = this.afp.getText();
    int afpInt = Integer.parseInt(afp);
    String otros = this.otros.getText();
    int otrosInt = Integer.parseInt(otros);

    double saludD = sueldoInt * (saludInt * 0.01);
    double afpD = sueldoInt * (afpInt * 0.01);

    int calcular = (int) (sueldoInt - saludD - afpD - otrosInt);

   JOptionPane.showMessageDialog(null,+calcular);     
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Lazaro
  • 37
  • 5

1 Answers1

3

You can use a JFormattedTextField with a MaskFormatter:

MaskFormatter formatter= new MaskFormatter("########");
JFormattedTextField textField = new JFormattedTextField(formatter);
void
  • 7,760
  • 3
  • 25
  • 43