0

I have a question regarding how someone was to make a JOptionPane.showMesageDialog(); appear within a duration of time. For example, my program is supposed to ask about the user's favorite movie, Subsequently at least 15 seconds after the user answers, my program is supposed to ask "are you there?", giving them the option of replying with Yes, no, or maybe. How exactly can I do that? (if that's possible)

here's my code

```
if (null==moviename|| moviename.trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "You did not enter anything");
        } else {             JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");  
        }

String ques=JOptionPane.showInputDialog("Are you there? Yes? No? Maybe?");
  if (ques=="yes") {
  JOptionPane.showMessageDialog(null,"Great To hear!");
  } else if (ques=="no") {
  JOptionPane.showMessageDialog(null,"Thats odd..");
  } else if (ques=="maybe" || ques=="Maybe" ) {
      JOptionPane.showMessageDialog(null, "That was rhetorical...");
  }
      }
  ```
  • 2
    Before you do anything else, read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. For a delayed Swing action, look into the [Timer](https://docs.oracle.com/en/java/javase/18/docs/api/java.desktop/javax/swing/Timer.html) class. – VGR Jun 29 '22 at 17:51

1 Answers1

-2

I belive you just need to add Thread.sleep(n); before the JOptionPane

remember to import: java.lang.Thread;

time is in ms so 15seconds is gonna be 15000.

if (null==moviename|| moviename.trim().isEmpty()) {
    try {  
        Thread.sleep(15000);
    } catch (InterruptedException e) {
        System.out.print("Sleep problem of type: "+e);
    }
    JOptionPane.showMessageDialog(null, "You did not enter anything");
    } else {             
    JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");  
    }
OSX_VINCH
  • 1
  • 1