disposing frame from swingWorker

前端 未结 1 899
攒了一身酷
攒了一身酷 2021-01-24 04:44

actually i have called the swing worker from a frame (Suppose) A.. in the swing worker class in do-in-Background method i have certain db queries and i am calling frame B too..

相关标签:
1条回答
  • 2021-01-24 05:34
    1. The done() method of the SwingWorker is usually overridden to display the final result. Upon completion of doInBackground() , the SwingWorker automaticlly invokes done() in the EDT. So put your frame's invisible and visible code in this function.

    2. The doInBackground() is not meant to do any GUI rendering task. You can invoke publish(V) from doInBackground() function which in turn invokes The process(V) method to run inside the EDT and performing GUI rendering task.

    So a sample solution would be:

    class Worker extends SwingWorker<Void, String> {
    
      JFrame frameA;
    
      public Worker(JFrame frameA) {
        this.frameA = frameA;
    
      }
    
      @Override
      protected void done() {
        frameA.dispose();
        new frameB().setVisible(true); 
    
      }
      //other code
    }
    

    Now, create you SwingWorker instance by passing the target frame to it's constructor: new Worker(frame); For your context you probably could make use of this

    However, you should not really design your application to be dependent on multiple JFrame. There are reasons for not to use multiple JFrame window. For more, see The Use of Multiple JFrames, Good/Bad Practice?. A general work around with use case where multiple frame would be needed is explained here.

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