Search code examples
javaswingjoptionpane

Trouble with JOptionPane and getting it to work


trying to create a joptionpane to work any ideas why this isnt working?

public void playerLost() {

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) {

                JOptionPane.YES_NO_OPTION(

        if (JOptionPane.NO_OPTION) {
            dispose();
        }
        else
        {
            game = new Game();
            createGridSquarePanels();
            update();
        }
}

i also tried this but says JOptionPane.NO_OPTION cannot be converted from int to string

    public void playerLost() {

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) {
        int choice = JOptionPane.showConfirmDialog(
        this, 
                "sorry you lost\nWould you like to restart?",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE);
        if (choice == JOptionPane.NO_OPTION) {
            dispose();
        }
        else
        {
            game = new Game();
            createGridSquarePanels();
            update();
        }
}
}

Solution

  • The first snippet doesn't compile as the syntax is invalid for using a JOptionPane.

    In the second piece of code, you're missing the title argument

    int choice = JOptionPane.showConfirmDialog(this, 
                    "sorry you lost\nWould you like to restart?",
                    "Title",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.INFORMATION_MESSAGE);
    

    Consult the JOptionPane javadoc