Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOptionPane cancel button won't cancel out of the window?

So, I'm making a simple guessing game, and the program won't exit out of the loop when the user presses the cancel button. Here's the loop

while(playAgain = true){
        int n = JOptionPane.showConfirmDialog(null, fields, "Number guessing game", JOptionPane.CANCEL_OPTION);

        if(n == JOptionPane.CANCEL_OPTION){
            playAgain = false;
        }

        int randomNumber = randomNumber();

        String guess = input.getText();
        compare(randomNumber, Integer.parseInt(guess));

    }
like image 468
TheSuds13 Avatar asked Jul 27 '15 06:07

TheSuds13


People also ask

How do I cancel my code on JOptionPane?

If it's really safe to just kill all JOptionPanes you can do something like this: public static void main(String[] args) { new Thread() { public void run() { for (int i = 0; i < 3; i++) { try { Thread. sleep(2000); } catch (InterruptedException e) { } JOptionPane. getRootFrame(). dispose(); } } }.

How do I disable the close button in JOptionPane?

Check the code below: JOptionPane pane = new JOptionPane("message"); JDialog dialog = pane. createDialog(null, "Title"); dialog. setDefaultCloseOperation(WindowConstants.


1 Answers

it should be

while(playAgain == true){

or

while(playAgain){

don't assign [=] true to playagain use comparison [==] . what you do is assign true to playagain and then check is it true.so it's always true

like image 77
Madhawa Priyashantha Avatar answered Sep 18 '22 10:09

Madhawa Priyashantha