spencerbro12
spencerbro12

Reputation: 21

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.

Upvotes: 1

Views: 1558

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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.

Upvotes: 2

James Black
James Black

Reputation: 41858

I think the problem is you have a close parenthesis in the wrong place.

else{

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

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

Before this while you should have a close parenthesis.

Also you never modify the riderHeight, so it will just stay in the loops forever.

You may want these two while loops to be if..then statements instead, as there doesn't seem to be any purpose in a while loop.

If you had

while(stillOnBike) {
}

Then you could have logic that would eventually exit as the person will eventually get off the bike, but you basically have while(true).

Upvotes: 2

Related Questions