NetBeans GUI Application keeps freezing when I try to run it

I am using NetBeans 6.9.1 programming, and I am currently working with the Swing GUI. Every time I run my program and click on the “calculate” button to determine the results, the program freezes. Here is the chunk of my code that causes it to freeze:

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String backTrouble;
    String heartTrouble;
    int riderHeight = Integer.parseInt(inputHeight.getText());

    backTrouble = inputBack.getText();
    heartTrouble = inputHeart.getText();

    while ((riderHeight >= 122) && (riderHeight <= 188)){

        if ((backTrouble.equals("N")) && (heartTrouble.equals("N"))){

            responseField.setText("It is OK for you to ride this roller coaster. Have fun!");
        }

        else if ((backTrouble.equals("Y")) && (heartTrouble.equals("N"))){

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

        else if ((backTrouble.equals("N")) && (heartTrouble.equals("Y"))){

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else{

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

    while ((riderHeight < 122) || (riderHeight > 188)){

        responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
    }
    }


}

I just don’t really understand why it keeps freezing, and some help would be appreciated, thank you.

Answer

Your while (such and such) loops are running continuously and blocking the Swing event thread, preventing the thread from painting the GUI and interacting with the user, effectively freezing your application. While these loops make sense in a console program, they don’t in an event-driven Swing GUI. Get rid of them, and the freezing will un-freeze. Perhaps you want to use if blocks instead here — hard to tell without more information.

So again perhaps if blocks would work better:

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String backTrouble;
    String heartTrouble;
    int riderHeight = Integer.parseInt(inputHeight.getText());
    backTrouble = inputBack.getText();
    heartTrouble = inputHeart.getText();

    // get rid of the while loop, 
    // while ((riderHeight >= 122) && (riderHeight <= 188)){
    if ((riderHeight >= 122) && (riderHeight <= 188)){
        if ((backTrouble.equals("N")) && (heartTrouble.equals("N"))){
            responseField.setText("It is OK for you to ride this roller coaster. Have fun!");
        }
        else if ((backTrouble.equals("Y")) && (heartTrouble.equals("N"))){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else if ((backTrouble.equals("N")) && (heartTrouble.equals("Y"))){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else{
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

        // again, no while loops here
        // while ((riderHeight < 122) || (riderHeight > 188)){
        if ((riderHeight < 122) || (riderHeight > 188)){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
    }
}

But I’m not 100% sure.

Leave a Reply

Your email address will not be published. Required fields are marked *