Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
May 8, 2002

Set the Cursor for Your Components


All AWT component classes have a setCursor() method that allows for dynamically setting the cursor for a particular component and any subcomponents.

For example, if you set the cursor for a Panel, all the components added to that Panel will inherit the Panel's cursor unless the components explicitly override it. Once a cursor is set for a component, the cursor will be automatically used when the pointer enters the component (as long as the component is visible and enabled).

To set the cursor for a component, get an instance of java.awt.Cursor. This class contains many predefined cursors that are accessed using type constants such as WAIT_CURSOR or HAND_CURSOR. You can retrieve Cursor objects by using one of the constructors or the static method Cursor.getPredefinedCursor(). This code sample displays a busy cursor (such as a watch icon) while an operation completes:
 
myComponent.setCursor(Cursor.getPredefinedCursor
(Cursor.WAIT_CURSOR));
//do stuff
myComponent.setCursor(null);
//use default cursor inherited from parent Component

Note that these methods need to be called from within the Swing event thread. If you call them from another thread, use the SwingUtilities.invokeLater() method to do so. You can also call getCursor() on the component if you need to save and restore the previous cursor afterwards. Setting the cursor to null may not be appropriate for components that already have a custom cursor defined.
Ramneek Handa
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date