setvisible method in java swing hangs system

后端 未结 4 1882
后悔当初
后悔当初 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<String, Integer> worker = new SwingWorker<String, Integer>() {
      ...
    };
    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.

    0 讨论(0)
  • 2021-01-18 17:30

    camickr gives you correct answer. I want to add that you may not modify the UI outside the Event Dispatch Thread (as you do in #doInBackground), Swing is single threaded so violating this rule could lead to very tricky bugs and strange things in your UI.

    0 讨论(0)
  • 2021-01-18 17:31

    You are displaying a modal dialog so the background code can't execute until the dialog is closed.

    Add a System.out.println(...) statement after the setVisible and you will see it never executes.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题