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
Jul 26, 2002

Create Custom Cursors in Swing


Sometimes the provided Swing cursor types just don't meet the needs of your application. In those cases, you may want to consider creating your own, completely customized cursor.

To add a custom cursor to an AWT component (or Swing JComponent), you need to call createCustomCursor on the Toolkit object associated with that component. You'll also need to pass in the image you want to use, indicate the point on the image to be used as the "hotspot" (the actual location of the mouse relative to the top left of the cursor image when the cursor is in use), and a name for the cursor. Here's an example of a method that does this:
 
void setCustomCursor(Image theImage, Point theHotspot,
String theName, Component theComponent)
{ 
Toolkit tk = theComponent.getToolkit(); 
Cursor myCursor =  
tk.createCustomCursor(theImage,theHotspot,theName); 
theComponent.setCursor(myCursor); 
}

You should also be aware of a couple of other methods in Toolkit that will help your implementation work properly across a variety of platforms and devices. Toolkit.getBestCursorSize() returns a Dimension object that indicates the proper size of a cursor image on the current display device. While most JVM implementations will attempt to resize your cursor image if it is not acceptable, you can exercise greater control over the appearance by providing an image appropriate for the indicated size. Likewise, the Toolkit.getMaximumCursorColors() method provides the current limit on the bitdepth for your image to help you avoid having your image dithered.

Whether you want to use a custom cursor for increased functionality or just for fun, setCustomCursor() allows you to provide any type of cursor you like for your AWT or Swing-based GUI applications.
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