Add Confirm Dialog

Learn to ask the user if he wants to play again with the help of a ConfirmDialog.

Confirm dialogs

You have already used JOptionPane to show a message dialog. JOptionPane can also show a confirm dialog box that displays a question with “Yes” and “No” buttons.

Asking “Do you want to play again?”

The last thing to add to this game is to let the user know he finished the game and ask him if he wants to play again.

Every time a light button is clicked, Framed will need to check whether the game is over.

  1. In initGUI(), call endGameIfDone() at the end of the actionPerformed() method.
    Note: Do not click the run button yet. Doing so now will cause a syntax error because endGameIfDone() does not exist yet.
    ...
 
  public void initGUI() {
 
          ...
 
        lightButton[row][col].addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            LightButton button = (LightButton)e.getSource();
            int row = button.getRow();
            int col = button.getCol();
            toggleLights(row, col);
    
            // check if the game is over
            __________();
          }
        });
          ...

Get hands-on with 1200+ tech skills courses.