:?: hello everyone, id like to ask for some help. i am a beginner in Java programming. i am doing mastermind game using java. the whole logic of the game is already finished, and my ONLY problem now is :?: HOW will i be able to repeat the whole game after the user press the YES Button from the message box asking him/her if he/she wants to play again...this makes it hard for me to finish the program. hope u guys can help me. thanks a lot :)

Dani AI

Generated

Short answer: do one of two safe things — either reset the existing game state and update the UI, or cleanly dispose the old frame and create a fresh one on the Event Dispatch Thread. Recreating a GUI without properly stopping listeners/threads or without using the EDT is a common cause of the exceptions you mention.

As described, you only need the program to start a new round after the Yes button; as hinted, creating another GUI instance can work but requires care. A cleaner pattern is to ask the user with a confirm dialog and call a resetGame() method that reinitializes the model and clears the view. Example flow:

int ans = JOptionPane.showConfirmDialog(mainFrame,
    "Play again?", "Confirm", JOptionPane.YES_NO_OPTION);
if (ans == JOptionPane.YES_OPTION) {
    resetGame();
}

And a sketch of resetGame():

private void resetGame() {
    // model: regenerate secret, clear guesses, reset counters
    secret = generateSecret();
    guesses.clear();
    attempts = 0;

    // view: update components on EDT
    SwingUtilities.invokeLater(() -> {
        inputField.setText("");
        feedbackLabel.setText("");
        guessListModel.clear();
        enableGameControls(true);
    });
}

If you prefer to create a fresh frame, dispose the old one and create the new window on the EDT to avoid race conditions:

frame.dispose();
SwingUtilities.invokeLater(() -> new MastermindFrame().setVisible(true));

Troubleshooting tips: check the exception stack trace — common causes are accesses to disposed components, background threads still touching the UI, or Swing calls off the EDT. Use frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) if you want to reuse the JVM, and stop any worker threads before disposing. For Swing threading rules and JOptionPane details see the Oracle Swing tutorials and API docs: Swing Concurrency Tutorial and JOptionPane.

Recommended Answers

All 4 Replies

I usually make another object of the gui class ... but I dont know how to get the running object closed .. cuz when I close the running object .. it throws an exception.....

where do u specifically declare the object of that same gui class?

I usually make another object of the gui class ... but I dont know how to get the running object closed .. cuz when I close the running object .. it throws an exception.....

i would like to try creating an object of that GUI class, but where will i create it? i mean where can i replace that object?

you should make the object in the button's action listener ... it will popup another frame of your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.