Coping with slow construction of GUI elements
- From: RedGrittyBrick <RedGrittyBrick@xxxxxxxxxxxxx>
- Date: Tue, 13 Nov 2007 17:24:06 +0000
Qs:
* How best to prevent blocking of EDT by slow GUI stuff?!
* How best to set and reset GUI cursor shapes?
In an ActionListener associated with a JMenuItem I had
actionPerformed(actionEvent e) {
switchPanel(new ComplexPanel());
}
However ComplexPanel was taking a couple of seconds to build. During this time the user is faced with an opened menu with an option selected and nothing happening onscreen. This is undesirable!
This had me puzzled for a while. Since I'm used to moving non-GUI work out of the EDT, it seemed like I needed to move some GUI work out of the EDT! Catch 22. I decided to postpone it instead, so the actionperformed() could return and the menu could then erase itself immediately.
actionPerformed(actionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
switchPanel(new ComplexPanel());
}
});
}
* Is this a normal idiom?
Anyway, the result is better but in order to let the user know the app was busy I wanted to set an hourglass cursor
actionPerformed(actionEvent e) {
//final Cursor oldCursor = frame.getCursor();
//frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Cursor oldCursor = frame.getCursor();
frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
switchPanel(new ComplexPanel());
frame.setCursor(oldCursor);
}
});
}
Originally I set the cursor ASAP, but I dislike having set and reset separated in different methods so I changed it as shown. It seems OK
* Any gotchas I've missed?
One thing I'm not entirely happy with in a multithreaded application is that some other thread might conceivably change the cursor after I've stashed oldCursor. In which case restoring it might be inappropriate. I'm pretty sure that my app wont do this (I'll disable other menu items so that other threads can't be changing the cursor concurrently)
* Is there a better design for managing cursor state?
.
- Follow-Ups:
- Re: Coping with slow construction of GUI elements
- From: bcr666
- Re: Coping with slow construction of GUI elements
- From: RedGrittyBrick
- Re: Coping with slow construction of GUI elements
- Prev by Date: Re: JTable, JScrollPane & horizontal scroll bars - why?
- Next by Date: Re: Coping with slow construction of GUI elements
- Previous by thread: Buffered Image color help
- Next by thread: Re: Coping with slow construction of GUI elements
- Index(es):
Relevant Pages
|
|