2

I have an app that sometimes kicks off a long-running process in a background thread. If it does this from the main app, I set the wait cursor like this:

oldCursor = App.getInstance().getGlassPane().getCursor();
App.getInstance().getGlassPane().setVisible(true);
App.getInstance().getGlassPane().setCursor(waitCursor);

This works, and I turn off the cursor like this:

App.getInstance().getGlassPane().setCursor(oldCursor);
App.getInstance().getGlassPane().setVisible(false);

So, now I sometimes do a long-running task from a JDialog. (it has setModal(true)) Doing this in the JDialog never changes the cursor:

oldCursor = getGlassPane().getCursor();
getGlassPane().setVisible(true);
getGlassPane().setCursor(waitCursor);

So, I tried setting it for the App, and that didn't work either.

Is there some way to get this working?

Xharze
  • 2,703
  • 2
  • 17
  • 30
CasaDelGato
  • 603
  • 7
  • 17
  • 3
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 10 '12 at 21:56
  • 1
    Is there a reason you're setting the GlassPane's cursor instead of the JFrame's or the content pane? – Raceimaztion May 10 '12 at 22:40
  • As I said, I tried the one in the JDialog, as there isn't a JFrame there. that didn't work. I'll try setting it in the ContentPane to see if that works. – CasaDelGato May 11 '12 at 14:38
  • Nope, didn't work. As for posting a SSCCE - I take it that means the approach of setting the cursor on the glasspane SHOULD work, but I must be doing something else wrong? If so, that is helpful info right there. Making a SSCCE is quite difficult in this case, as there is a fair amount of UI being generated from data, in a large complex app. It would likely take me a full day of work to create a SSCCE - in which case it's not financially sane to do so. Not having a wait cursor is an annoyance, not a show stopping bug. – CasaDelGato May 11 '12 at 14:48

1 Answers1

0

I know this question is super old. However, I had the same question, and it took me a while of searching through old threads on many websites before I was able to find the solution. Posting it here to help me (and maybe others) find it in the future.

For reference, https://www.javaspecialists.eu/archive/Issue065-Wait-Cursor-Wait.html explains why the OP's code doesn't work. Unfortunately, the solutions presented in that article are less than ideal (e.g., set the cursor before the dialog opens, then change back after it closes).

However, since the JDialog is a top-level Swing container, you can call GetRootPane() directly on it to get access to changing the cursor:

getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// long running code here
getRootPane().setCursor(Cursor.getDefaultCursor());

Another improvement is to set the wait cursor in a try block, and to revert the default cursor in the finally, so you're not left with a forever wait cursor if there's an exception:

try {
    getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    // long running code here
} finally {
    getRootPane().setCursor(Cursor.getDefaultCursor());
}
dbc
  • 677
  • 8
  • 21