setvisible method in java swing hangs system

后端 未结 4 1884
后悔当初
后悔当初 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:37

    First, it is recommended to do all the GUI updates in the Swing Event-Dispatch thread, i.e. using the SwingUtilites class.

    Second, your JDialog is modal and so blocks the thread in which the setVisible(true) method is called (in your case the Main thread, in the following case the Swing Event-Dispatch Thread).

    I do not say the following code is perfect, but it should put you on the track...

    
    final JDialog waitForTrans = new JDialog((JFrame) null, true);
    
    SwingWorker worker = new SwingWorker() {
    
      public String doInBackground() throws Exception {
        Thread.sleep(5000);
        return null;
      }
    
      public void done() {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            waitForTrans.setVisible(false);
            waitForTrans.dispose();
          }
        });
      }
    
    };
    
    worker.execute();
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        waitForTrans.add(new JLabel("Please Wait..."));
        waitForTrans.setMinimumSize(new Dimension(300, 100));
        waitForTrans.setVisible(true);
      }
    });
    
    

    Hope this helps.

提交回复
热议问题