JDialog displaying progress

A

andrej.zirko

Hi,
I'm trying to display on modal JDialog progress of copying files. I've
used SwingWorker for copying the files, so it won't freeze the JDialog,
but then I need to get the response to parent frame (if the files were
copied succesfully). I've tried StringWorker.get but when I use this
method the JDialog won't show up :(

//Creating dialog from MainFrm and executing download method
final CopyDlg copyDlg = new CopyDlg(this, true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
copyDlg.setVisible(true);
}
});

try {
sWorker = copyDlg.download(toCopy,
currentDirectory.getPath(), remoteDirectories, ftp, jLogTxtArea);
System.out.println(sWorker.get()); //without this line
it's working ok, but I don't get response :\
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//method download in CopyDlg class
public SwingWorker download(final ArrayList foDs, final String
where, final FDArrayList directories, FtpClient ftp, JTextArea
jLogTxtArea) throws InterruptedException {
this.ftp = ftp;
this.jLogTxtArea = jLogTxtArea;


SwingWorker sWorker = new SwingWorker<Boolean, Void>() {
public Boolean doInBackground() throws Exception {
String foD;

for (int x = 0; x < foDs.size(); x++){
foD = (String) foDs.get(x);

if (!downloadFoD(foD, where, directories))
return false;
}

return true;
}

public void done(){
setVisible(false);
}
};

sWorker.execute();

// try {
// System.out.println(sWorker.get());
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// } catch (ExecutionException ex) {
// ex.printStackTrace();
// }

return sWorker;
}

I'm newbie to Java so please be patient :)
Any help is appreciated
Thanks in advance ;)
 
H

hiwa

Hi,
I'm trying to display on modal JDialog progress of copying files. I've
used SwingWorker for copying the files, so it won't freeze the JDialog,
but then I need to get the response to parent frame (if the files were
copied succesfully). I've tried StringWorker.get but when I use this
method the JDialog won't show up :(

//Creating dialog from MainFrm and executing download method
final CopyDlg copyDlg = new CopyDlg(this, true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
copyDlg.setVisible(true);
}
});

try {
sWorker = copyDlg.download(toCopy,
currentDirectory.getPath(), remoteDirectories, ftp, jLogTxtArea);
System.out.println(sWorker.get()); //without this line
it's working ok, but I don't get response :\
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//method download in CopyDlg class
public SwingWorker download(final ArrayList foDs, final String
where, final FDArrayList directories, FtpClient ftp, JTextArea
jLogTxtArea) throws InterruptedException {
this.ftp = ftp;
this.jLogTxtArea = jLogTxtArea;


SwingWorker sWorker = new SwingWorker<Boolean, Void>() {
public Boolean doInBackground() throws Exception {
String foD;

for (int x = 0; x < foDs.size(); x++){
foD = (String) foDs.get(x);

if (!downloadFoD(foD, where, directories))
return false;
}

return true;
}

public void done(){
setVisible(false);
}
};

sWorker.execute();

// try {
// System.out.println(sWorker.get());
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// } catch (ExecutionException ex) {
// ex.printStackTrace();
// }

return sWorker;
}

I'm newbie to Java so please be patient :)
Any help is appreciated
Thanks in advance ;)
Read the API documentation of the SwingWorker.get() method and try
calling the get() method from within the SwingWorker.done() method.
That should be a peaceful and safe way for getting the result from a
long task.
 
A

andrej.zirko

Thanks works fine ;)
I've done it like this

public void done(){
setVisible(false);
((MainFrm)getParent()).showResponse(this, "Can't copy
selected items!", false);
}

But I have another question :)

I've read on java tutorials, that I should also create my dialog in
invokeLater, but how can I do that? :\ The CopyDlg has to be final, if
I want to use him in invokelater. If I'll put just declaration of
CopyDlg outside the invokeLater statement, It won't be final any more.
And if I'll put declaration with creation inside invokeLater statement,
I can't invoke download or upload method outside invokeLater :\
I'm really confused here - please help ;) :)

BTW Everything works fine now, but I just want to have it as it should
be. :)

hiwa napísal(a):
 
H

hiwa

Thanks works fine ;)
I've done it like this

public void done(){
setVisible(false);
((MainFrm)getParent()).showResponse(this, "Can't copy
selected items!", false);
}

But I have another question :)

I've read on java tutorials, that I should also create my dialog in
invokeLater, but how can I do that? :\ The CopyDlg has to be final, if
I want to use him in invokelater. If I'll put just declaration of
CopyDlg outside the invokeLater statement, It won't be final any more.
And if I'll put declaration with creation inside invokeLater statement,
I can't invoke download or upload method outside invokeLater :\
I'm really confused here - please help ;) :)

BTW Everything works fine now, but I just want to have it as it should
be. :)

hiwa napísal(a):
Sorry, I don't understand your story...
Could you show some code example or a pseudo code?
Talking about code without SSCCE is quite inefficient.
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html
 
A

andrej.zirko

Sorry, I don't understand your story...
Could you show some code example or a pseudo code?
Talking about code without SSCCE is quite inefficient.
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html

I'm still talking about the code in my first post
//Creating dialog from MainFrm and executing download method
final CopyDlg copyDlg = new CopyDlg(this, true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//I WANT TO HAVE CREATION OF COPYDLG HERE - new CopyDlg(this, true);
copyDlg.setVisible(true);
}
});

try {
sWorker = copyDlg.download(toCopy,
currentDirectory.getPath(), remoteDirectories, ftp, jLogTxtArea);
System.out.println(sWorker.get()); //without this line
it's working ok, but I don't get response :\
} catch (InterruptedException ex) {
ex.printStackTrace();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top