In Swing, is there a way to extract a predefined mouse cursor Image from the toolkit?

房东的猫 提交于 2019-11-29 15:27:55

I'm not sure this is the best solution in your case, because a good built-in mouse cursor should be the best. Anyway you can use mouse listeners and draw on a glasspane according to the mouse position. Here's a glasspane drawing example.

In general, no. Most cursors are owned by the platform's host operating system, but a few live in $JAVA_HOME/lib/images/cursors/, for example:

$ ls -1 lib/images/cursors/
cursors.properties
invalid32x32.gif
motif_CopyDrop32x32.gif
motif_CopyNoDrop32x32.gif
motif_LinkDrop32x32.gif
motif_LinkNoDrop32x32.gif
motif_MoveDrop32x32.gif
motif_MoveNoDrop32x32.gif
lvr123

Java uses the default system cursor except for the Drag-and-Drop cursor, where it is using it's own cursors.

So for every cursors but DnD, refer to Extract cursor image in Java . JNA has to be used and can be easily added to any Maven project.

For DnD cursors, the solution from trashgod is good. They can be loaded this way:

private static Path customSystemCursorPath = null;

public static Image loadDnDCursorImage(String cursorName) throws IOException {

    if (customSystemCursorPath == null) {
        String jhome = System.getProperty("java.home", "????");
        customSystemCursorPath = Paths.get(jhome, "lib", "images", "cursors");
    }

    // TODO change this to retrieve the cursor filename from  cursors.properties
    cursorName = "win32_" + cursorName + "32x32.gif";

    Path cursorPath = customSystemCursorPath.resolve(cursorName);

    Image image = ImageIO.read(cursorPath.toFile());
    return image;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!