You can't put such statements inside a class directly. They must be inside a method. That's why the code doesn't compile if you uncomment these statements.
These statements shouldn't be inside the worker at all. They should be in the enclosing method, just before the worker is executed. Indeed, you want to initialize the progress dialog and make it visible only once, before the background process is started. And you can't do that in doInBackground()
, because every access to Swing components must be done in the event dispatch thread, and not in the background thread.
The same goes for the call to JOptionPane.showMessageDialog()
: it shouldn't be inside the doInBackground()
method, since it must be done in the event dispatch thread. It shoud be inside the done()
method, which should call get()
to see if the background process was successful or not.
And the code in done() doesn't need to be enclosed into SwingUtilities.invokeLater()
, since done()
is executed in the event dispatch thread.
Finally, you should respect the Java naming conventions: variables start with a lower-case letter.