Sunday, 15 September 2013

Swing GUI freezes for unknown reason

Swing GUI freezes for unknown reason

I'm creating a java program that creates lineups for Grand Prix/Pinewood
Derby like races and then once scores are entered finds the finalists and
assigns them a lane/heat number.
So far everything in the program does exactly what I want it to do until I
reach the part where I'm trying to create the finalists. After I click the
button/press enter to enter in the final score, my GUI freezes. Here is
the code right before the freeze:
//allows user to enter the places for each car in each heat in each round
void enterScores() {
if(indexCount == 0 || (indexCount) % numLanes == 0) {
textArea.append("\nRound " + (roundCount + 1) + " Heat " +
(heatCount+1) + ": ");
}
userResponse.setText("");
prompt.setText(holderNameArray[roundCount][indexCount] + ": ");
textArea.append("\n" + holderNameArray[roundCount][indexCount] + ": ");
userResponse.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
holderScoreArray[roundCount][indexCount] =
Integer.parseInt(userResponse.getText());
textArea.append("" + holderScoreArray[roundCount][indexCount]);
indexCount++;
for(ActionListener act : enter.getActionListeners()) {
enter.removeActionListener(act);
}
for(ActionListener act : userResponse.getActionListeners()) {
userResponse.removeActionListener(act);
}
repeatEnterScores();
}
});
enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
holderScoreArray[roundCount][indexCount] =
Integer.parseInt(userResponse.getText());
textArea.append("" + holderScoreArray[roundCount][indexCount]);
indexCount++;
for(ActionListener act : enter.getActionListeners()) {
enter.removeActionListener(act);
}
for(ActionListener act : userResponse.getActionListeners()) {
userResponse.removeActionListener(act);
}
repeatEnterScores();
}
});
}
//helps repeat enterScores() due to anon class restrictions
//checks to change the number of heats/rounds
void repeatEnterScores() {
if((indexCount) % numLanes == 0 || indexCount == numCars) {
heatCount++;
}
if(indexCount == numCars) {
indexCount = 0;
heatCount = 0;
roundCount++;
}
if(roundCount < numRounds) {
enterScores();
} else {
textArea.setText("You may now click the Scores tab to see the
scores you have just entered.");
scoresPanelSetup();
}
}
It used to not freeze and continue, but would freeze in a later place
instead. I'm a beginner so I'm not sure why it's freezing here now. If you
need any more code/information, please let me know. Whenever I debug it
steps through all of my code fine, it's just the gui having problems.
Thanks!

No comments:

Post a Comment