How to prompt a confirmation dialog box in the middle of non event dispatching thread

后端 未结 6 631
滥情空心
滥情空心 2021-01-19 00:09

I have the following fun which will be executed by non event dispatching thread. In the middle of thread, I want a

  1. A confirmation box pop up. Thre
6条回答
  •  半阙折子戏
    2021-01-19 00:11

    Launch the JOptionPane from the EDT, and pass the value back with a FutureTask.

    public int fun()
    {
        FutureTask dialogTask = new FutureTask(new Callable() 
        {
            @Override public Integer call() 
            {
                return JOptionPane.showConfirmDialog(SaveToCloudJDialog.this, message, title, JOptionPane.YES_NO_OPTION);
            }
        });
        SwingUtilities.invokeLater(dialogTask);
        int choice  = dialogTask.get();
    }
    

    Credit to jtahlborn How to pass results from EDT back to a different thread?

提交回复
热议问题