5

There is a JTabbedPane In my Swing program. When user clicks on a tab, the program takes a while to get the data and process the results, then shows the results in the selected tab.

How can I display a hour glass, or something of that effect so that user knows it's processing data? Not to click on the tab again before it finishes it job.

Line
  • 1,397
  • 3
  • 14
  • 39
Frank
  • 29,646
  • 56
  • 159
  • 233

5 Answers5

26

The simplest way is to just call setCursor on the appropriate component (probably the top-level window) with the appropriate Cursor.

component.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

And then set it back when you are done.

component.setCursor(Cursor.getDefaultCursor());
Nathan
  • 7,243
  • 6
  • 46
  • 67
Dan Dyer
  • 52,995
  • 17
  • 128
  • 165
6

A JProgressBar (possibly in indetermiante mode) sounds right - put that on the tab until the data has been fetched. A well-designed UI shouldn't force the user to wait for long-running tasks to complete and instead allow them to do something else inbetween.

Michael Borgwardt
  • 335,521
  • 76
  • 467
  • 706
3

setCursor(int) is deprecated. This is probably a bit cleaner:

setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Hermann Hans
  • 1,778
  • 1
  • 13
  • 23
0

As the other answers mention, you can set a wait cursor, but you also mention preventing additional mouse clicks. You can use a glass pane to prevent clicks on components until the long operation is finished. In addition to the Sun tutorials on the glass pane, there is a nice example at http://www.java2s.com/Code/Java/Swing-JFC/DemonstrateuseofGlassPane.htm

clartaq
  • 5,168
  • 3
  • 39
  • 46
0

I would as other mentioned

  • Change the cursor
  • Use a SwingWorker
  • Display the progressbar or an animated image in the glasspane
  • Hide the glasspane when the task is completed
  • Restore the default cursor