Reputation: 43
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.. in the done() method however i want to dispose the frame A.. how can i do that..? i cannot write dispose() in frame A class because that results in disposing of frame before the new frame(frame B) is visible... Please help!!
class frameA extends JFrame{
public frameA(){
//done some operations..
SwingWorker worker=new Worker();
worker.execute();
}
public static void main(string[] args){
new frameA();
}
}
and in worker class
class Worker extends SwingWorker<Void, String> {
public Worker() {
super();
}
//Executed on the Event Dispatch Thread after the doInBackground method is finished
@Override
protected void done() {
//want to dispose the frameA here..
}
@Override
protected Void doInBackground() throws Exception {
// some db queries
new frameB().setVisible(true);
// call to frameb
}
Upvotes: 1
Views: 881
Reputation: 15438
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.
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.
Upvotes: 3