• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change cursor

 
Melki Joe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a button for which i have set the hand symbol cursor by default. On clicking the button I should change the cursor symbol to hour glass until a particular process is made. Currently if I click the button the cursor is not changing it remains the same as the hand symbol but after a while at the end of processing it changes for a fraction of a second and returns to the normal cursor.

format of my code which i have used.

button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
...
...
public void actionPerformed(ActionEvent evt)
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR ) );
method();
setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
}
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would cause the same behaviour.

You are right about splitting the method call and the resetting of the cursor into a later event, but you'll want to keep the first cursor change in the current event.

(and @Override is invalid there - you are implementing, not overriding )


Now, if method() is quite time consuming, using a SwingWorker would be better:

You can use publish and an overridden implementation of process to "fire" updates while performing method() - you publish in doInBackground, and handle user interface updates for them in process.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
(and @Override is invalid there - you are implementing, not overriding )

Ahh! Thats just the eclipse thing. It always shows up with the annotation.

Originally posted by Rob Prime:
That would cause the same behaviour.


I don't get this.
The invokeLater is supposed to queue it up on the EDT.
So the cursor is reset and repainted.
Method called and executed.
Cursor reset and repainted.

Won't these be sequential calls?
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting all three lines of code will cause them to be executed in the same order at the same speed as when they are executed directly. You are just postponing the method calls.


In the original code:
actionPerformed:
- set cursor to wait
- method()
- set cursor to default
- (system) update user interface to reflect the above calls

When all are moved to one single Runnable that is invoked later:
actionPerformed:
- schedule the Runnable for a later time
- (system) update user interface to reflect the above calls - oh wait, there aren't any
Runnable (at a later time:
- set cursor to wait
- method()
- set cursor to default
- (system) update user interface to reflect the above calls

That's why the first line, setting the cursor to wait, should be split from the rest:
actionPerformed:
- set cursor to wait
- schedule the Runnable for a later time
- (system) update user interface to reflect the above calls
Runnable (at a later time:
- method()
- set cursor to default
- (system) update user interface to reflect the above calls

As you can see, there is a user interface update between the first and second call now. With both other options there isn't.
 
Melki Joe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your ideas but I think Swing worker is only available in jdk1.6 but I am using jdk1.5.

Thank you.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can implement something similar quite easy:

execute() starts a new thread that calls doInBackground(). When this is done it will call done() on the Event Dispatcher Thread. Just like SwingWorker.

Here's a simple solution for process / publish as well:

Not as efficient as SwingWorker, which caches the published objects until the Event Dispatcher Thread has time. With SwingWorker, the size of the list in process can be larger than the number of arguments of the last call to publish.
[ December 08, 2008: Message edited by: Rob Prime ]
 
Melki Joe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your solution I am working on it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!