JDialog displaying progress

Discussion in 'Java' started by andrej.zirko@gmail.com, Dec 17, 2006.

  1. Guest

    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 ;)
     
    , Dec 17, 2006
    #1
    1. Advertising

  2. hiwa Guest

    wrote:
    > 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.
     
    hiwa, Dec 17, 2006
    #2
    1. Advertising

  3. Guest

    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):
    > wrote:
    > > 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.
     
    , Dec 17, 2006
    #3
  4. hiwa Guest

    wrote:
    > 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):
    > > wrote:
    > > > 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.

    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
     
    hiwa, Dec 18, 2006
    #4
  5. Guest

    > 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();
    }
     
    , Dec 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Tillotson
    Replies:
    2
    Views:
    435
    Chris Tillotson
    Nov 4, 2003
  2. Bruce Whitehouse
    Replies:
    3
    Views:
    406
    Alvin Bruney
    Nov 23, 2003
  3. Charlie Zhang
    Replies:
    3
    Views:
    1,297
    Paul Lutus
    Aug 16, 2004
  4. Scott Steiner
    Replies:
    0
    Views:
    463
    Scott Steiner
    May 9, 2005
  5. Replies:
    4
    Views:
    1,644
    steve
    Feb 24, 2006
Loading...

Share This Page