Coping with slow construction of GUI elements



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?
.



Relevant Pages

  • Re: busy cursor
    ... What I want to do is display an hour glass while the application is performing its calculations. ... Problem is that if I hover my cursor over another window and then back over the original window, ... public void actionPerformed ... say I have the main class that constructs a gui. ...
    (comp.lang.java.gui)
  • Re: busy cursor
    ... Simple to make just an hour glass that doesn't rotate - use setCursor. ... Problem is that if I hover my cursor over another window and then back over the original window, ... public void actionPerformed ... say I have the main class that constructs a gui. ...
    (comp.lang.java.gui)
  • Re: Great SWT Program
    ... A SPECIFIC APP suck. ... Not the general concept of a GUI, ... reposition the cursor when the click it received had given it focus ... What you described isn't bad GUI behavior; ...
    (comp.lang.java.programmer)
  • Re: busy cursor
    ... Simple to make just an hour glass that doesn't rotate - use setCursor. ... Problem is that if I hover my cursor over another window and then back over the original window, ... It is recommended to run any swing components from the Event Dispatch Thread the Swing thread. ... say I have the main class that constructs a gui. ...
    (comp.lang.java.gui)
  • Re: Coping with slow construction of GUI elements
    ... How best to set and reset GUI cursor shapes? ... In an ActionListener associated with a JMenuItem I had ...
    (comp.lang.java.gui)