Possible Duplicate:
SwingWorker in Java
I have several classes that need to work together but, they're not.
For one, I have Main:
public class Main
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
//JFrame dummy for JDialog errors
modal = new JFrame();
new PrimaryErrorDialog("Title", "Message", e, false);
JFrame masterWindow = new JFrame();
masterWindow.setVisible();
}
}
}
}
It creates PrimaryErrorDialog class:
private JDialog dialog = this;
private JTextArea text;
private JPanel buttonContainer;
private JButton sendErrorReportT, sendErrorReportF;
public PrimaryErrorDialog(String title, String message,
final Exception error, final boolean exit)
{
super(Main.modal, title, true);
//JButton for sending error report
sendErrorReportT = new JButton("Send Error Report");
sendErrorReportT.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.dispose();
dialog = new JDialog(Main.modal, "Sending...", true);
Worker thread = new Worker(error);
thread.start();
JProgressBar progress = new JProgressBar();
progress.setIndeterminate(true);
dialog.add(progress);
dialog.pack();
dialog.setVisible(true);
}
});
//JButton for not sending error report
sendErrorReportF = new JButton("Don't send error report");
sendErrorReportF.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
dialog.dispose();
if (exit)
{
System.exit(0);
}
}
});
buttonContainer = new JPanel();
buttonContainer.add(sendErrorReportT);
buttonContainer.add(sendErrorReportF);
add(text);
add(buttonContainer);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void cleanUpAndContinue()
{
dialog.dispose();
dialog = new JDialog(Main.modal, "title", true);
dialog.add(/*Jbutton with listener that disposes this dialog*/)
dialog.setVisible(true);
}
It calls thw Worker class that extends Thread:
public class Worker extends Thread
{
Exception e;
public Worker(Exception error)
{
e = error;
}
@Override
public void run()
{
//do something that takes time
PrimaryErrorDialog.cleanUpAndContinue();
}
It goes back to PrimaryErrorDialog, which would then have to inform the user that task was completed, and then terminate that dialog.
Then we go back to main where masterWindow is created.
All of this code is executed prior to creation of masterWindow, because this segment is ran when an error occurrs (if LAF is not present, .proprties files are missing, etc)...
That's why I created the dummy JFrame, so JDialog can attach to it, I didn't wan't to make it a JFrame.
This code is also executed later in program, for "real" runtime errors, some classes just have a bit different parameters and/or constructors.
However, this does not work, I have tried it in milion ways, i tried with SwingWorker, nothing seems to do what I want. Usually the email code is not even reached, or program doesn't wait for the dialogs to be disposed...
And what do I want?
Error occurrs. Dialog pops up telling me an error occurred and asking me if I want to send an error report. I say no - dialog closes, taking down the whole program if error is fatal. I say yes - dialog closes, new one opens with indetermined progressbar, WHILE email with stacktrace is being sent in the background. Email gets sent, dialog with progressbar closes and a new one opens telling my my error report was sent. I press ok and dialog closes, taking down the whole program if error is fatal, else, continues where it left of from Main class.
Note: An error can occurr in any class, it doesn't be from Main only...
I've not read you're previous question, however...
The basic problem you have is needing to notify the dialog (which is within the EDT) that the background thread has completed (as well as provide feedback to the user if possible).
This is covered in the Concurrency in Swing lesson.
Below is a simple proof of concept. Personal, I would normally construct a custom panel and put it onto the a
JDialog
, but this just a proof of concept.dirty hack wrap
dialog.setVisible(true);
intoinvokeLater()
use
SwingWorker
orRunnable#Thread
instead or plainThread
is there issue that you ignored answers and especially comments from your previous question
Swing Gui doesn't care about output from Thread, because there isn't implemented notifiers for Event Dispatch Thread, you have to invoke that by invokeLater, or to use
SwingWorker
,SwingWorkers
methodsdone
,publish
andprogress
can notify EDT correctly