0

I have a program that plays an audio and at the same time asks for user input using JFrame. If the user inputs a correct response (31) 3 times, then the program will continue with other aspects of the code (I haven't included this as it is not relevant). However, if the user fails to provide the correct response 3 times within 30 seconds, the dialog is supposed to close and then continue with the other aspect. In the following program, if the user fails to enter correct response, the JFrame dialog continues to show up. How do I make the JFrame close in 30 second. If I can make the JFrame close after 30 seconds, I plan on hardcoding the countconsolablecry1 to 3 after the closing so that it can pass the while loop and continue with the program. Please help me close the dialog.

public class crytask {

public static void main(String args[]) throws InterruptedException, IOException {

    Runnable consolablecry1 = new consolablecry();
    final Thread consolablethread1 = new Thread(consolablecry1);
    consolablethread1.start();
    final long timestartconsolablecry1 = System.nanoTime();
    int countconsolablecry1 = 0;
    TimeUnit.SECONDS.sleep(5);
    while(countconsolablecry1!=3){      
        JFrame frameconsolablecry1 = new JFrame();
        Object resultconsolablecry1 = JOptionPane.showInputDialog(frameconsolablecry1, "Enter sequence:");
        String inputconsolablecry1 = resultconsolablecry1.toString();               
        if (inputconsolablecry1.equalsIgnoreCase("31")) {
            countconsolablecry1 = countconsolablecry1 +1;}}
    consolablethread1.interrupt();
    final long timeendconsolablecry1 = System.nanoTime();
    System.out.println("this thread is cancelled");
    TimeUnit.SECONDS.sleep(45-(timeendconsolablecry1-timestartconsolablecry1)/1000000000);            
}}
 class consolablecry implements Runnable {
@Override
public void run() {  
    Clip clip = null;
    try {       
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("/Users/babe/Desktop/Con1.wav").getAbsoluteFile());
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
        Thread.sleep(clip.getMicrosecondLength() / 1000);
        TimeUnit.SECONDS.wait(5);
    } catch(InterruptedException ex) {
        clip.stop();
        System.out.println("Cancelled 1!");
    } catch (Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();}}}
rookie
  • 63
  • 1
  • 5
  • *"..asks for user input using JFrame."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) For this, use either `JOptionPane.showInputDialog(..)` or `JDialog`. – Andrew Thompson Aug 01 '15 at 01:55
  • You're potentially violating the single thread rules of Swing. The simple solution is to use a Swing `Timer` and return the required result back to the call when the dialog is closed. As one possible [example](http://stackoverflow.com/questions/22979504/closing-a-runnable-joptionpane/22979571#22979571) – MadProgrammer Aug 01 '15 at 03:27

0 Answers0