Swingworker - Is it possible?

Discussion in 'Java' started by Edsoncv, Feb 10, 2009.

  1. Edsoncv

    Edsoncv Guest

    Hello
    Recently I have implemented a swingworker dialog in order to display
    a progressbar and some text while some math computations are
    happening. It works fine except for one thing: I want a particular
    behavior and do not know if it can be handled with swingworker, maybe
    the experts can help me:

    In my GUI I use a actionPerformed method to start a long task
    (Swingworker), in this task I perform some calculations and update my
    JprogressBar and a JTextArea. But I want some results to be displayed
    right after the task is done(with printSolveMessages()), but if I call
    this printing function in the next line it does not print the last
    calculation result (in fact the initial value for the variables),
    since the calculation is running in another thread. I tried the
    swingworker.get() method, but it freezes all the GUI and the
    progressbar is not updated. Any clues of how to do that?

    The actionperformed class is like this:

    public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == startButton){
    startButton.setEnabled(false);
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    // task is the swingworker
    task.addPropertyChangeListener(this);
    task.execute();
    /* the task is executing and I want a pause before
    print the results with the function below, otherwhise I get
    old results*/
    printSolveMessages();

    I also tried a while(!pMon.isDone()){} right before printSolveMessages
    (); but it also locks the screen upate.

    pMon is a object that tracks the start and the end of the task.
    Edsoncv, Feb 10, 2009
    #1
    1. Advertising

  2. Edsoncv wrote:
    > Hello
    > Recently I have implemented a swingworker dialog in order to display
    > a progressbar and some text while some math computations are
    > happening. It works fine except for one thing: I want a particular
    > behavior and do not know if it can be handled with swingworker, maybe
    > the experts can help me:
    >
    > In my GUI I use a actionPerformed method to start a long task
    > (Swingworker), in this task I perform some calculations and update my
    > JprogressBar and a JTextArea. But I want some results to be displayed
    > right after the task is done(with printSolveMessages()), but if I call
    > this printing function in the next line it does not print the last
    > calculation result (in fact the initial value for the variables),
    > since the calculation is running in another thread. I tried the
    > swingworker.get() method, but it freezes all the GUI and the
    > progressbar is not updated. Any clues of how to do that?
    >
    > The actionperformed class is like this:
    >
    > public void actionPerformed(ActionEvent evt) {
    > if (evt.getSource() == startButton){
    > startButton.setEnabled(false);
    > setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    > // task is the swingworker
    > task.addPropertyChangeListener(this);
    > task.execute();
    > /* the task is executing and I want a pause before
    > print the results with the function below, otherwhise I get
    > old results*/
    > printSolveMessages();
    >
    > I also tried a while(!pMon.isDone()){} right before printSolveMessages
    > (); but it also locks the screen upate.
    >
    > pMon is a object that tracks the start and the end of the task.
    >


    You are blocking the EDT. You can't do that if you want the GUI to
    update. Just start another thread and run printSolveMessages() in that
    thread. If it needs to update the GUI then use a SwingWorker for to run
    it in.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Feb 10, 2009
    #2
    1. Advertising

  3. In article <4990ca1b$0$25581$>,
    Knute Johnson <> wrote:

    > Edsoncv wrote:
    > > Recently I have implemented a swingworker dialog in order to
    > > display a progressbar and some text while some math computations
    > > are happening. It works fine except for one thing: I want a
    > > particular behavior and do not know if it can be handled with
    > > swingworker, maybe the experts can help me:
    > >
    > > In my GUI I use a actionPerformed method to start a long task
    > > (Swingworker), in this task I perform some calculations and update
    > > my JprogressBar and a JTextArea. But I want some results to be
    > > displayed right after the task is done(with printSolveMessages()),
    > > but if I call this printing function in the next line it does not
    > > print the last calculation result (in fact the initial value for
    > > the variables), since the calculation is running in another thread.
    > > I tried the swingworker.get() method, but it freezes all the GUI
    > > and the progressbar is not updated. Any clues of how to do that?
    > >
    > > The actionperformed class is like this:
    > >
    > > public void actionPerformed(ActionEvent evt) {
    > > if (evt.getSource() == startButton){
    > > startButton.setEnabled(false);
    > > setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    > > // task is the swingworker
    > > task.addPropertyChangeListener(this);
    > > task.execute();
    > > /* the task is executing and I want a pause before print the
    > > results with the function below, otherwhise I get old results */
    > > printSolveMessages();
    > >
    > > I also tried a while(!pMon.isDone()){} right before printSolveMessages
    > > (); but it also locks the screen upate.
    > >
    > > pMon is a object that tracks the start and the end of the task.

    >
    > You are blocking the EDT. You can't do that if you want the GUI to
    > update. Just start another thread and run printSolveMessages() in
    > that thread. If it needs to update the GUI then use a SwingWorker
    > for to run it in.


    Knute's right. If you haven't already seen it, you might look at the
    PrimeNumbersDemo application, as an example:

    <https://swingworker.dev.java.net/>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Feb 10, 2009
    #3
  4. Edsoncv

    blue indigo Guest

    On Mon, 5641 Sep 1993 16:28:12 -0800, Knute Johnson wrote:

    > Edsoncv wrote:
    >> In my GUI I use a actionPerformed method to start a long task
    >> (Swingworker), in this task I perform some calculations and update my
    >> JprogressBar and a JTextArea. But I want some results to be displayed
    >> right after the task is done(with printSolveMessages()), but if I call
    >> this printing function in the next line it does not print the last
    >> calculation result (in fact the initial value for the variables),
    >> since the calculation is running in another thread. I tried the
    >> swingworker.get() method, but it freezes all the GUI and the
    >> progressbar is not updated. Any clues of how to do that?

    >
    > You are blocking the EDT. You can't do that if you want the GUI to
    > update. Just start another thread and run printSolveMessages() in that
    > thread. If it needs to update the GUI then use a SwingWorker for to run
    > it in.


    SwingWorker supplies a done() method that it calls on the EDT when it
    is done. It is ready-made for this purpose, so the OP can just implement
    the done() method in his(?) SwingWorker to update the GUI with this
    information when the SwingWorker is done. It can use get(), which
    shouldn't block when called from done().

    http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html#done()

    --
    blue indigo
    UA Telecom since 1987
    blue indigo, Feb 10, 2009
    #4
  5. Edsoncv

    Edsoncv Guest

    On 10 fev, 00:37, blue indigo
    <> wrote:
    > On Mon, 5641 Sep 1993 16:28:12 -0800, Knute Johnson wrote:
    > >Edsoncvwrote:
    > >> In my GUI I use a actionPerformed method to start a long task
    > >> (Swingworker), in this task I perform some calculations and update my
    > >> JprogressBar and a JTextArea. But I want some results to be displayed
    > >> right after the task is done(with printSolveMessages()), but if I call
    > >> this printing function in the next line it does not print the last
    > >> calculation result (in fact the initial value for the variables),
    > >> since the calculation is running in another thread. I tried the
    > >> swingworker.get() method, but it freezes all the GUI and the
    > >> progressbar is not updated. Any clues of how to do that?

    >
    > > You are blocking the EDT. You can't do that if you want the GUI to
    > > update. Just start another thread and run printSolveMessages() in that
    > > thread. If it needs to update the GUI then use a SwingWorker for to run
    > > it in.

    >
    > SwingWorker supplies a done() method that it calls on the EDT when it
    > is done. It is ready-made for this purpose, so the OP can just implement
    > the done() method in his(?) SwingWorker to update the GUI with this
    > information when the SwingWorker is done. It can use get(), which
    > shouldn't block when called from done().
    >
    > http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html#done()
    >
    > --
    > blue indigo
    > UA Telecom since 1987


    Thanks a lot for the sugestions.
    Playing a little a found also a different solution. In the property
    change listener I did the following:

    public void propertyChange(PropertyChangeEvent evt) {
    if (pMon.isDone()){
    startButton.setEnabled(true);
    setCursor(null);
    /* Here I only print the results if the progressMonitor is finished,
    so it is the last action to be done /*
    printSolveMessages();

    }
    if ("progress" == evt.getPropertyName()) {
    int progress = (Integer) evt.getNewValue();
    int progressOld = (Integer) evt.getOldValue();
    if (progress >= progressOld){
    progressBar.setValue(progress);

    }
    }
    }
    Edsoncv, Feb 10, 2009
    #5
  6. Edsoncv

    Lew Guest

    Edsoncv wrote:
    >    Playing a little a found also a different solution. In the property
    > change listener I did the following:
    >
    >   public void propertyChange(PropertyChangeEvent evt) {
    >                 if (pMon.isDone()){


    Your indentation is far too wide for Usenet. Don't use TABs, and
    don't indent more than four spaces.

    >                         startButton.setEnabled(true);
    >                         setCursor(null);
    > /* Here I only print the results if the progressMonitor is finished,
    > so it is the last action to be done /*
    >                          printSolveMessages();
    >
    >                 }
    >                 if ("progress" == evt.getPropertyName()) {


    This equality test will fail, usually. You used the wrong equality
    test.

    >                     int progress = (Integer) evt.getNewValue();
    >                     int progressOld = (Integer) evt..getOldValue();
    >                     if (progress >= progressOld){


    Strictly speaking, you only need to set a new value on the progress
    bar if the new value exceeds the old value; if they're equal there's
    not much point in resetting the value.

    >                         progressBar.setValue(progress);
    >                     }
    >                 }
    >             }


    --
    Lew
    Lew, Feb 10, 2009
    #6
  7. Edsoncv

    Roedy Green Guest

    On Mon, 9 Feb 2009 16:05:28 -0800 (PST), Edsoncv <>
    wrote, quoted or indirectly quoted someone who said :

    > swingworker dialog


    see http://mindprod.com/jgloss/swingworker.html

    The key thing you must understand is it runs all your code on the EDT
    thread. This means you have to be VERY quick. If you dawdle in the
    least your entire GUI will freeze.

    If you need to do time-consuming tasks, they need to run on some other
    threads. see http://mindprod.com/jgloss/thread.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Here is a point of no return after which warming becomes unstoppable
    and we are probably going to sail right through it.
    It is the point at which anthropogenic (human-caused) warming triggers
    huge releases of carbon dioxide from warming oceans, or similar releases
    of both carbon dioxide and methane from melting permafrost, or both.
    Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
    ~ Gwynne Dyer
    Roedy Green, Feb 11, 2009
    #7
  8. Roedy Green wrote:
    > On Mon, 9 Feb 2009 16:05:28 -0800 (PST), Edsoncv <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> swingworker dialog

    >
    > see http://mindprod.com/jgloss/swingworker.html
    >
    > The key thing you must understand is it runs all your code on the EDT
    > thread.


    For a moment there I thought "it" referred to "swingworker" in the prior
    sentences!


    > This means you have to be VERY quick. If you dawdle in the
    > least your entire GUI will freeze.
    >
    > If you need to do time-consuming tasks, they need to run on some other
    > threads. see http://mindprod.com/jgloss/thread.html



    The API docs for SwingWorker may help also:

    "There are three threads involved in the life cycle of a SwingWorker :

    Current thread: The execute() method is called on this thread. It
    schedules SwingWorker for the execution on a worker thread and returns
    immediately. One can wait for the SwingWorker to complete using the get
    methods.

    Worker thread: The doInBackground() method is called on this
    thread. This is where all background activities should happen. To notify
    PropertyChangeListeners about bound properties changes use the
    firePropertyChange and getPropertyChangeSupport() methods. By default
    there are two bound properties available: state and progress.

    Event Dispatch Thread: All Swing related activities occur on this
    thread. SwingWorker invokes the process and done() methods and notifies
    any PropertyChangeListeners on this thread."


    I usually do slow stuff in doInBackground() and update the GUI in
    done(). The former is not executed on the EDT, the latter is.


    --
    RGB
    RedGrittyBrick, Feb 11, 2009
    #8
    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. Alex

    SwingWorker

    Alex, Feb 8, 2004, in forum: Java
    Replies:
    2
    Views:
    1,178
  2. Sebastian Millies

    SwingWorker and inherited thread priorities

    Sebastian Millies, Sep 12, 2005, in forum: Java
    Replies:
    5
    Views:
    2,319
    Thomas Hawtin
    Sep 12, 2005
  3. Replies:
    3
    Views:
    723
  4. Royan
    Replies:
    2
    Views:
    592
    Royan
    Apr 1, 2008
  5. Hendrik Maryns
    Replies:
    2
    Views:
    4,937
    RedGrittyBrick
    May 16, 2008
Loading...

Share This Page