3

Is it possible to get hold of a standard AWT Cursor in the form of a bitmap image (e.g. BufferedImage) or anything paintable on a Graphics2D? For example, the text cursor new Cursor(Cursor.TEXT_CURSOR).

I believe these cursors change according to native platform, and I would like to incorporate them in some Swing icons.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0__
  • 66,707
  • 21
  • 171
  • 266
  • 1
    No, to my knowledge it is not possible. *"I would like to incorporate them in some Swing icons."* That would be confusing to the user so I **strongly** recommend against it. In fact, it would be far better to have something that is recognizable as a pointer, while being distinctly *different* to any cursor the system uses. – Andrew Thompson May 03 '13 at 12:30
  • 1
    You can't do it using pure Java, though technically speaking you can if you resort to platform-dependant native code (see http://stackoverflow.com/questions/739870/extract-cursor-image-in-java.) But as Andrew mentioned, I'd really advise against it in this case - incorporating cursors into icons is a complete recipe for a UI disaster. – Michael Berry May 03 '13 at 12:48
  • @AndrewThompson well I have a tool bar where some icons correspond to the cursor that is used by the tool (e.g. text cursor, east-west resize etc.) I see little risk of confusion here. Many applications do so. E.g. have a look at the toolbar in Photoshop. – 0__ May 03 '13 at 14:24

1 Answers1

3

Even if you use your own icon, you can display the platform-dependent cursor that you will later use in a given context. Hover over the button and the adjacent panel to see the effect. A tooltip on the component using the icon is always a good touch.

image

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

/**
 * @see http://stackoverflow.com/a/16363072/230513
 */
public class Test {

    private static class CursorPanel extends JPanel {

        private static final Icon ICON = UIManager.getIcon("html.pendingImage");
        private Cursor cursor;

        public CursorPanel(final Cursor cursor) {
            this.cursor = cursor;
            String name = cursor.getName();
            JButton button = new JButton(name, ICON);
            button.setCursor(cursor);
            button.setToolTipText(name);
            this.add(button);
            final JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(200, 125);
                }
            };
            panel.setCursor(cursor);
            panel.setBorder(new TitledBorder(name));
            this.add(panel);
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(0, 1));
        f.add(new CursorPanel(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)));
        f.add(new CursorPanel(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045