Exception for Java Swing application?

For some reason, this blocks the Java application. Did I handle the exception correctly?

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt)    {                                             
    double amount, interest,rateCalc, a, b, c, payment;
    int years, months;
    while (true){
        try{
            amount = Double.valueOf(loanAmount.getText());
            interest = Double.valueOf(interestRate.getText());
            years = Integer.valueOf(loanYears.getText());
            rateCalc = (interest/12);
            months = (years*12);
            a = Math.pow((1+rateCalc),months);
            b = (a*rateCalc);
            c = (a-1);
            payment = (amount *(b/c));
            monthlyPayment.setText("Mortgage Payment $ = " + payment);

        } catch (NumberFormatException nfe){
            javax.swing.JOptionPane.showMessageDialog(null,
                    "Please enter numbers and not letters");
            return;
        }
    }

}

monthPayment is returned to the java application.

+3
source share
2 answers

You create a loop in Thread Dispatch Thread. This will cause your drawing flow to begin to rotate, which makes it feel like your application is hanging and does not allow you to perform any other actions with the graphical interface.

I would delete the loop while(true).

+2
source

Try to remove the while (true) statement. In your case, this is not necessary.

0
source

Source: https://habr.com/ru/post/1747698/


All Articles