disposing frame from swingWorker

南楼画角 提交于 2019-12-02 04:35:24
Sage
  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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!