Using Thread.sleep(..) and Swing Components

Discussion in 'Java' started by karpthomas@googlemail.com, Sep 29, 2008.

  1. Guest

    Hi,

    I want to update a component several times.
    When the user clicks a button, another component
    - e.g. a JProgressBar, but it seems that this doesn't matter -
    should be updated several times.

    The code looks like following:

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {
    for (int i = 0; i <= 10; i++) {
    try {
    Thread.sleep(300);
    progressBar.setValue(i);
    } catch (InterruptedException e) {
    // do something
    }
    }
    }

    The problem is, that the component seems to be repainted only, when
    the method
    is finished.

    What am I doing wrong?

    Regards, Thomas
     
    , Sep 29, 2008
    #1
    1. Advertising

  2. Peter Duniho wrote:
    > On Mon, 29 Sep 2008 10:30:37 -0700, <> wrote:
    >
    >> [...]
    >> The problem is, that the component seems to be repainted only, when
    >> the method
    >> is finished.
    >>
    >> What am I doing wrong?

    >
    > You are delaying the event-dispatch thread by calling sleep().
    >
    > Operations that take any significant amount of time should be handled on
    > a different thread, using the EventQueue.invokeLater() and
    > EventQueue.invokeAndWait() methods to execute any code that is required
    > to be executed on the EDT (such as setting the value of a progress bar
    > control).
    >


    I find SwingWorker useful too. It simplifies the work of creating and
    using threads when you also want to update the GUI.


    Just my ¤0.02 worth

    --
    RGB
     
    RedGrittyBrick, Sep 29, 2008
    #2
    1. Advertising

  3. Guest

    Hi,

    thank you. I think I'll have to learn about threads a
    little bit more...

    But using SwingWorker should be OK.

    Thank you, Regards, Thomas
     
    , Sep 29, 2008
    #3
  4. In article <gbr79n$o17$>,
    RedGrittyBrick <> wrote:

    > Peter Duniho wrote:
    > > On Mon, 29 Sep 2008 10:30:37 -0700, <> wrote:
    > >
    > >> [...]
    > >> The problem is, that the component seems to be repainted only,
    > >> when the method is finished.
    > >>
    > >> What am I doing wrong?

    > >
    > > You are delaying the event-dispatch thread by calling sleep().
    > >
    > > Operations that take any significant amount of time should be
    > > handled on a different thread, using the EventQueue.invokeLater()
    > > and EventQueue.invokeAndWait() methods to execute any code that is
    > > required to be executed on the EDT (such as setting the value of a
    > > progress bar control).

    >
    > I find SwingWorker useful too. It simplifies the work of creating and
    > using threads when you also want to update the GUI.


    Peter and RGB are correct. In addition, a SwingWorker back-port is
    available, and it includes a nice example using JProgressBar:

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

    Alternatively, if your model is trivial and your drawing is simple, a
    javax.swing.Timer may suffice:

    <http://groups.google.com/group/comp.lang.java.help/msg/8d88d9d0bc7d947a>

    Compare this with Knute Johnson's approach based on Runnable:

    <http://groups.google.com/group/comp.lang.java.help/msg/fbe351cb311c9637>

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Sep 29, 2008
    #4
  5. Daniel Pitts Guest

    wrote:
    > Hi,
    >
    > I want to update a component several times.
    > When the user clicks a button, another component
    > - e.g. a JProgressBar, but it seems that this doesn't matter -
    > should be updated several times.
    >
    > The code looks like following:
    >
    > private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {
    > for (int i = 0; i <= 10; i++) {
    > try {
    > Thread.sleep(300);
    > progressBar.setValue(i);
    > } catch (InterruptedException e) {
    > // do something
    > }
    > }
    > }
    >
    > The problem is, that the component seems to be repainted only, when
    > the method
    > is finished.
    >
    > What am I doing wrong?
    >
    > Regards, Thomas

    All GUI work happens on the same Thread, in a queue. Including
    re-painting of components. Nothing can be repainted until your method
    completes.

    Instead of using Thread.sleep, you should consider using a
    javax.swing.Timer or a SwingWorker.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 4, 2008
    #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. Stephen Miller
    Replies:
    3
    Views:
    3,983
    Stephen Miller
    Jul 2, 2004
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,447
  3. mkrause
    Replies:
    0
    Views:
    676
    mkrause
    May 6, 2005
  4. Sam Roberts
    Replies:
    16
    Views:
    387
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    237
    Richard
    May 22, 2007
Loading...

Share This Page