setvisible method in java swing hangs system

后端 未结 4 1912
后悔当初
后悔当初 2021-01-18 17:20

I have banking gui application that I am currently working on and there seems to be a problem with the setvisible method for my jdialog. After the user has withdrawn a valid

4条回答
  •  梦谈多话
    2021-01-18 17:29

    setVisible is a method that affects the GUI, causing something to be shown (and, in the case of a modal dialog like yours, block until the dialog is closed). It (like everything else that modifies the visible UI) should never be called except on the Swing event dispatch thread. You're calling it from the doInBackground method of SwingWorker, which runs on a background thread.

    What you need to do to fix this is make the waitForClose dialog a final variable that you create before calling execute on the SwingWorker and then call setVisible on immediately after starting the worker.

    final JDialog waitForTrans = ...
    // set up the dialog here
    
    SwingWorker worker = new SwingWorker() {
      ...
    };
    worker.execute(); // start the background process
    
    waitForTrans.setVisible(true); // show the dialog
    

    You need to do it in this order because otherwise the modal dialog will block you from starting the worker.

提交回复
热议问题