Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #294

Re: art of writing a resp

From "Wesley Hall" <wesley.hall@THRWHITE.remove-dii-this>
Subject Re: art of writing a resp
Message-ID <4570a559$0$8743$ed2619ec@ptn-nntp-reader02.plus.net> (permalink)
Newsgroups comp.lang.java.gui
References <1164948778.020923.279510@j72g2000cwa.googlegroups.com>
Date 2011-04-27 15:26 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.programmer
Timasmith wrote:
> Having played around with someones variant of Swingworker I have found
> pleasing results with using a background thread for actions initiated
> by a table, tree or menu.  Clicking on the said item kicks off the
> action while letting the gui items respond appropriately.  In fact
> rather than hardcoding it everywhere I extended an ActionListener to do
> just that.

Just remember that Swing is NOT thread safe so any updates you do to
visual components should be done on the AWT thread. It is OK to use a
thread to go off and fetch data etc, but when the data returns and you
want to display it you must use SwingUtilities.invokeLater to update the
GUI as you will get occasional strange behaviour.

> Now when it comes to buttons there is a different story.  When you
> press a button you want a good solid push but with the background
> thread I get a bouncy button that immediately springs back to its
> position - too quickly in my estimation.  Since the action it is
> performing may be quick or slow its difficult to guage the slow ones
> only for background processing.
> 
> I guess I could introduce an artifical pause for the button but any
> other ideas on that.

Not really, it has been a while since I did any swing but presumably the
button stays 'pressed' for as long as the action listener is running. If
you are making that run very short with threading, it is not suprising
that this is happening.

You would be better of forking a thread to go a fetch data from the
server and having the action listener prepare the GUI for when the data
comes back, so if you are hitting a button to display a dialog, build
the dialog in memory on the AWT thread while your worker goes of and
fetches the data. If your GUI builds before your data comes back use the
classes in java.util.concurrent to synchronize the threads, then use
invokeLater to pass the data to the dialog, display the dialog and
return from the action listener.

In doing this, you are finding a balance between single threading (where
the time delay is building gui time + collecting data time), and doing
everything in another thread (which, on the AWT thread is almost 0 time
and makes your button look like it isnt doing anything). What you have
instead is a button that remains depressed for however long the data
fetch takes (without gui building time which is happening concurrently).

You could also consider showing the dialog as soon as it is built and
have the fields populate when the data comes back. This will give your
application a very responsive real (with all clicks responding visually
in a fraction of a second) but you could end up with dialogs that look
empty for a few seconds if the dialog builds much before the data is
returned.


> Is there a way to flip the hourglass regardless of the control you are
> waving over?  Gets a little complex perhaps as you might want to
> hourglass one frame but not another.

I seem to remember hourglass cursors being a bit of a pain in the neck.
There was a nice java tip on javaworld that enabled you to push an
action handler on the event queue so that any event that took over x
amount of time would automatically flip the cursor. This was much nicer
than doing it manually for every button click... lets see if I can find
it...


Yup... here it is... its a bit old now (published in 2000) but should do
the job. There could be a more modern solution, but I am a little out of
the loop as everyone seems to want web apps these days...

http://www.javaworld.com/javaworld/javatips/jw-javatip87.html

Have fun.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

art of writing a responsi "Timasmith" <timasmith@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
  Re: art of writing a resp "Wesley Hall" <wesley.hall@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
    Re: art of writing a resp "Timasmith" <timasmith@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
    Re: art of writing a resp "Fred Kleinschmidt" <fred.kleinschmidt@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
  Re: art of writing a resp "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000
    Re: art of writing a resp "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:26 +0000

csiph-web