Tuesday, January 11, 2005

Setting cursor for entire application

The secret is that you have to change the cursor on theoriginating component AND on its frame!Here's a little code snippet (part of a static utility class) that does the job :

public class Util {

public static void doWaitCursor (Component component) {
setCursor(Cursor.WAIT_CURSOR, component);
}

public static void noWaitCursor (Component component) {
setCursor(Cursor.DEFAULT_CURSOR, component);
}

public static void setCursor (int cursor, Component component) {
component.setCursor(Cursor.getPredefinedCursor(cursor));
Frame frame = getFrame(component);
if (frame != null)
frame.setCursor(Cursor.getPredefinedCursor(cursor));
}

public static Frame getFrame (Component c) {
if (c instanceof Frame)
return (Frame) c;
while ((c = c.getParent()) != null)
if (c instanceof Frame)
return (Frame) c;

return null;
}
}

Source: http://forum.java.sun.com/thread.jspa?threadID=111793&messageID=296338

No comments: