Java Forum / GUI / November 2006
How Do I Make My Application "Feel" Faster
Jason Cavett - 29 Nov 2006 20:31 GMT I'm currently designign a GUI and, while it's going pretty well, I've noticed when I do something like open an document using the GUI, the drop down menu (File > Open) that I've created doesn't immediately disappear. It seems to hang there for a moment and refreshes after the document is starting to open. This happens with other things as well.
I tried turning on Swing's built in double-buffering on the menus, but that doesn't seem to help any. I have also searched around on the web to see if there was any information on how to fix this problem.
Basically, I'm wondering - how do I make my app "feel" faster so user's don't get the impression that the application is lagging (even though it really isn't).
Thanks
J. David Boyd - 29 Nov 2006 21:30 GMT > I'm currently designign a GUI and, while it's going pretty well, I've > noticed when I do something like open an document using the GUI, the [quoted text clipped - 9 lines] > don't get the impression that the application is lagging (even though > it really isn't). Are you using multiple threads: 1 for the GUI, one for the file processing?
Steve W. Jackson - 29 Nov 2006 21:33 GMT > I'm currently designign a GUI and, while it's going pretty well, I've > noticed when I do something like open an document using the GUI, the [quoted text clipped - 11 lines] > > Thanks Sounds like you're looking to "manage perceptions" for someone using your app, which is good.
If you do everything that File>Open implies inside the actionPerformed method of an ActionListener, which executes on the Event Dispatch Thread (EDT), then the repainting of the menu and other GUI components occurs later. This can give the appearance of slowness, in my experience, whether it really is slow or not.
One step I've taken is to have that actionPerformed method create a separate thread to do its work. That lets the method itself complete quicker, thus allowing the GUI to update its appearance, redrawing the menus, etc. Much of what's done in that separate thread is not likely to require use of the EDT anyway, which is good.
The gotcha sometimes comes when the action *does* need to do some things on the EDT, from what I've seen. If I know that to be the case, I try experimenting to find my options. Often, I can launch that thread with SwingUtilities.invokeLater (which should be EventQueue.invokeLater, since that's what it calls as of 1.3). That will queue the new Runnable and should let actionPerformed finish its work, along with anything else already on the EDT's queue.
= Steve =
 Signature Steve W. Jackson Montgomery, Alabama
Jason Cavett - 30 Nov 2006 14:09 GMT On Nov 29, 4:33 pm, "Steve W. Jackson" <stevewjack...@knology.net> wrote:
> In article <1164832296.233748.5...@j44g2000cwa.googlegroups.com>, > [quoted text clipped - 39 lines] > Steve W. Jackson > Montgomery, Alabama- Hide quoted text -- Show quoted text - That was a great explanation, thank you.
So, I attempted to create a thread in my actionPerformed with the following code:
Thread appThread = new Thread() { public void run() { try { model.openProject(); } catch (Exception e) { e.printStackTrace(); }
} };
appThread.run();
However, the menu still seems a little sluggish. Am I approaching this the right way? (I was never very strong with Threads, so things get a little fuzzy here for me.)
Andrew Thompson - 30 Nov 2006 14:36 GMT > On Nov 29, 4:33 pm, "Steve W. Jackson" <stevewjack...@knology.net> > wrote: > > In article <1164832296.233748.5...@j44g2000cwa.googlegroups.com>, ...
> > > I'm currently designign a GUI and, while it's going pretty well, I've > > > noticed when I do something like open an document using the GUI, the > > > drop down menu (File > Open) that I've created doesn't immediately > > > disappear. ....
> > If you do everything that File>Open implies inside the actionPerformed > > method of an ActionListener, which executes on the Event Dispatch Thread > > (EDT), .....
> > One step I've taken is to have that actionPerformed method create a > > separate thread to do its work. ...
> So, I attempted to create a thread in my actionPerformed with the > following code: [quoted text clipped - 3 lines] > try { > model.openProject(); ....
> However, the menu still seems a little sluggish. Am I approaching this > the right way? (I was never very strong with Threads, so things get a > little fuzzy here for me.) A simple test for whether the problem lies deeper is to do absolutely nothing in the action performed, just to check that the menu disappears quickly when there is no process behind it..
..as an aside, please replace tabs with 2 or 3 spaces before posting code to usenet, as some news clients, and web based interfaces to usenet, expand tabs to ridiculous widths.
Andrew T.
Steve W. Jackson - 30 Nov 2006 14:59 GMT [ snip ]
> So, I attempted to create a thread in my actionPerformed with the > following code: [quoted text clipped - 15 lines] > the right way? (I was never very strong with Threads, so things get a > little fuzzy here for me.) When you call the run method of a Thread object, it simply executes in line just like any other method. Instead, call "appThread.start()" in your code, which causes the Thread object to run in its own separate thread -- which was your goal. Then appThread will exist independently of your actionPerformed method and the EDT on which it was invoked, and will become eligible for garbage collection when its run method completes.
Alternatively, you could create a Runnable instead of a Thread. If you actually want to spawn it separately, you can then pass that Runnable as the parameter to a Thread constructor and call start on the Thread. But if you should need, as I did in some situations, to have it run on the EDT, you could pass that Runnable to EventQueue.invokeLater.
= Steve =
 Signature Steve W. Jackson Montgomery, Alabama
Jason Cavett - 30 Nov 2006 15:17 GMT On Nov 30, 9:59 am, "Steve W. Jackson" <stevewjack...@knology.net> wrote:
> In article <1164895786.940642.122...@n67g2000cwd.googlegroups.com>, > [quoted text clipped - 36 lines] > Steve W. Jackson > Montgomery, Alabama- Hide quoted text -- Show quoted text - That works great! :-) Thank you.
steve - 29 Nov 2006 22:16 GMT > I'm currently designign a GUI and, while it's going pretty well, I've > noticed when I do something like open an document using the GUI, the [quoted text clipped - 11 lines] > > Thanks your on the wrong thread!!.
instead of jumping to your subroutine, when you detect the menu selection, fire up another thread to process your subroutine and exit from the menu selection code, straight away.
Timasmith - 30 Nov 2006 03:45 GMT > I'm currently designign a GUI and, while it's going pretty well, I've > noticed when I do something like open an document using the GUI, the [quoted text clipped - 11 lines] > > Thanks Google swingworker (bundled in 1.6)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|