java - Changing the default cursor to busy cursor does not work as expected -
after many attempts trying make jprogressbar
work expected, became successful @ achieving goal. had used @madprogrammer's advice , used swingworker
program work want.
now, want cursor change busy_cursor http://telcontar.net/misc/screeniecursors/cursor%20hourglass%20white.png when jprogressbar
goes 0% 100%. i've googled , found out that
setcursor(cursor.getpredefinedcursor(cursor.wait_cursor));
is code it. i've tried not work expected.
relevant piece of code:
jprogressbar progress; jbutton button; jdialog dialog; //fields of gui class progress=new jprogressbar(jprogressbar.horizontal,0,100); button=new jbutton("done"); dialog=new jdialog(); //done methods progress.setvalue(0); progress.setstringpainted(true); progress.setborderpainted(true); //also done methods button.addactionlistener(this); //also done methods dialog.setlayout(new flowlayout(flowlayout.center)); dialog.settitle("please wait..."); dialog.setbounds(475,150,250,100); dialog.setmodal(true); //also done methods dialog.add(new jlabel("loading...")); dialog.add(progress); //also done methods
and here actionperformed
method:
public void actionperformed(actionevent e) { setcursor(cursor.getpredefinedcursor(cursor.wait_cursor)); task task=new task(); task.addpropertychangelistener(this); task.execute(); dialog.setvisible(true); }
the propertychange
method:
public void propertychange(propertychangeevent evt) { if("progress" == evt.getpropertyname()){ int progressnum = (integer) evt.getnewvalue(); progress.setvalue(progressnum); } }
and nested class task
:
class task extends swingworker<void, void> { /* * main task. executed in background thread. */ @override public void doinbackground() { int progressnum = 0; setprogress(0); while (progressnum < 100) { try { thread.sleep(10); } catch (interruptedexception ex) { system.err.println("an error occured:"+ex); ex.printstacktrace(); } progressnum ++; setprogress(math.min(progressnum, 100)); } return null; } /* * executed in event dispatching thread */ @override public void done() { //setcursor(null); //turn off wait cursor setcursor(cursor.getdefaultcursor()); //is 1 or 1 above right? dialog.dispose(); progress.setvalue(progress.getminimum()); } }
when press button
, jdialog jprogressbar appears , jprogressbar goes 0% 100%. during time, cursor needs changed busy_cursor http://telcontar.net/misc/screeniecursors/cursor%20hourglass%20white.png(busy cursor) , when jprogressbar reaches 100%, normal cursor(normal_cursor http://fc05.deviantart.net/fs70/i/2011/008/1/4/mouse_cursor_windows_by_mikima-d36oslj.png) needs restored.
the problem is, when press button
, cursor changes busy cursor split second , then, changes original cursor. want cursor busy until jprogressbar reaches 100%.
i've added code convert cursor busy cursor in actionperformed
method , restore normal cursor in done
method of nested class task
. note i've included necessary packages.
- what causing problem?
- how fix it?
should use
setcursor(null);
or
setcursor(cursor.getdefaultcursor());
to restore cursor?
when call setcursor()
on jframe
, has effect frame, not dialog. similarly, when call setcursor()
on jdialog
, has effect dialog, not frame. there problem: did not set cursor dialog, right?
i'm not sure whether setcursor(null)
safer setcursor(cursor.getdefaultcursor())
. :p