Search code examples
javamultithreadingtimerjoptionpane

How to dispose JOptionPane after x seconds


So I have a code like this:

    for (int i = 0; i < totalNumPlayers; i++) {

                runTimer(30, myTextArea);

                players.get(i).bet = JOptionPane.showInputDialog(players.get(i).name + ", please enter your bet: ");

            }

I need to auto-submit JOptionPane (with a default int value) after the timer expires.

Code of my timer:

    ScheduledExecutorService scheduledExecutorService =
            Executors.newScheduledThreadPool(1);

    ScheduledFuture scheduledFuture = scheduledExecutorService.schedule((Callable) () -> {

        for (int j = 1; j <= duration; j++) {

            myTextArea.replaceRange("\n" + String.valueOf(j), myTextArea.getText().lastIndexOf("\n"), myTextArea.getText().length());

            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Timer error!");
            }
        }
        return "Called!";

     }, 2, TimeUnit.SECONDS);

     scheduledExecutorService.shutdown();

Solution

  • Actually, there is a good approach here closing-joptionpane-ShowInternalOptionDialog-programmatically

    Modifying for your case specifically:

    import javax.swing.JOptionPane;
    
    public class Example {
        static String bet = "";
        public static void main(String[] args) {
            final JOptionPane pane = new JOptionPane();
    
            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pane.getRootFrame().dispose();
    
                }
            });
            t1.start();
            bet = pane.showInputDialog("give me a value");
    
            if(bet == null)
                bet = "30";
    
    
            System.out.println(bet);
            System.exit(0);
        }
    }
    

    If the user gives no input, JOptionPane makes the String bet = null. So you check on that, and if the String is null you simply assign your own value to it.

    Also, as I said in the comments, you can achieve the same thing with a Timer.

    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    import java.awt.event.ActionListener;
    import java.awt.event.*;
    
    public class StackOverFlow {
        static String bet = "";
        public static void main(String[] args) {
            final JOptionPane pane = new JOptionPane();
    
            Timer t = new Timer(3000, new ActionListener() {
                public void actionPerformed(ActionEvent e ) {
                    pane.getRootFrame().dispose();
                }
            });
            t.start();
            bet = pane.showInputDialog("give me a value");
            t.stop();
    
            if(bet == null) {
                bet = "30";
            }
    
            System.out.println(bet);
        }
    }
    

    Both ways achieve the same thing. The value 30 obviously can be given by a declared constant.