Search code examples
javaswingcursor

Change/Display Cursor with Text


I am currently trying to change the cursor shown on my JFrame to some text like "wait please your job is being done" while a certain button 's actionPerformed() method is executing. The only solution I have found as of yet is to change the Cursor with an Image which contains my desired Text. The code is below:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Hasikome extends JFrame{

static Cursor cursor;

private static final long serialVersionUID = 1L;

public static void main(String[] args) {

        final Hasikome hasi = new Hasikome();
        hasi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hasi.setSize(new Dimension(1200, 1200));
        hasi.setPreferredSize(new Dimension(500, 500));
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension dim = kit.getBestCursorSize(16, 16);
        BufferedImage buffered = null;
        try {
            buffered = ImageIO.read(new File(Paths.get("").toAbsolutePath().toString() + "\\belge.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        java.awt.Shape circle = new Ellipse2D.Float(0, 0, dim.width - 1, dim.height - 1);
        Graphics2D g = buffered.createGraphics();
        g.setColor(Color.BLUE);
        g.draw(circle);
        g.setColor(Color.RED);
        hasi.add(new JButton(new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                hasi.setCursor(cursor);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                } finally {
                    hasi.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            }
        }), BorderLayout.NORTH);
        int centerX = (dim.width - 1) /2;
        int centerY = (dim.height - 1) / 2;
        g.drawLine(centerX, 0, centerX, dim.height - 1);
        g.drawLine(0, centerY, dim.height - 1, centerY);
        g.dispose();
        cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");
        hasi.pack();
        hasi.setVisible(true);
    }
}

The problem with this code is Dimension dim = kit.getBestCursorSize(16, 16); this line always generates Cursor with size 32x32 on Windows and it is platform dependant. I can't use values more than 32x32 or else I get exception for the line cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor"); . And because I use a reasonably long text (250x16) it won't allow me to show the text image as Cursor properly.

This solution not necessary, what I want to accomplish is to show text to users as Cursor while some Button 's actionPerformed() method is being executed. Is there any way I can do this? Thanks in advance.


Solution

  • Check out Disabled Glass Pane for one approach.

    If allows you to display the Glass Pane with text painted on a semi transparent background while using the "wait" cursor.