1

I am writing an event for button.In that ,i have used swingworker in the way that i use execute method in this button code.Everything works fine but the problem is that i want to display a panel just after the execute method is finished.What is happening is that panel appears before the execute method finishes its task.Here is my code.Kindly help me howto display the panel once the execute method is finished with its task

               Main f2=new Main();
               f2.getfile(FileName,0);
               f2.execute();
               Panel.setVisible(true);
               Panel.setSize(815, 587);

edited: Well i have found a solution, for this if anyone faces the same problem.I have created a method(call it showPanel) in GUI class and when the SwingWorker done method is called then in that done method i call the showPanel method for showing the panel.

Xara
  • 8,748
  • 16
  • 52
  • 82

1 Answers1

2

The SwingWorker performs its work in another Thread (called worker thread in the class javadoc) then the EDT, and allows to update the Event Dispatch Thread during/after its execution.

The reason why your panel becomes visible before the execute method has finished is clearly mentioned in the class Javadoc of the SwingWorker class, which is a must-read:

The execute() method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately

See this answer for an example usage of a SwingWorker, or consult the Swing concurrency tutorial for more information.

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • _SwingWorker will perform its background task on the EDT, and block your UI_ ehhh ... no. The execute can be called from _any_ thread (simply read your citation, with emphasis on _schedules_ and _worker thread_ :) It doesn't block the calling thread, which is what @Zara is observing: the code line below the execute()` is executed immediately – kleopatra Apr 03 '12 at 10:05
  • @kleopatra You are completely right. I had another (incorrect) version of this answer before I posted it, and thought I corrected everything after reading the SwingWorker javadoc. The behavior I described probably occurs when you call the run method iso the execute method. I removed the incorrect parts from my answer – Robin Apr 03 '12 at 10:10