JButton default cursor

Is there a way to set the default cursor for JButtoncomponents?

Here's how to set the cursor for one JButton:

JButton btn = new JButton("Click me");
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));

According to lookAndFeel Nimbus defaults there is no property like "Button.cursor".

I would like to set the default cursor once so that all JButtons in the application have the same manual cursor when the mouse cursor moves.

+4
source share
1 answer

You may have custom buttonone that extends JButtonand uses this. Sort of:

MyCustomJButton.java

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class MyCustomJButton extends JButton implements MouseListener
{

    private Cursor defaultCursor;
    private Cursor handCursor;

    public MyCustomJButton()
    {
        super();

        init();
    }

    public MyCustomJButton(Action a)
    {
        super(a);

        init();
    }

    public MyCustomJButton(Icon icon)
    {
        super(icon);

        init();
    }

    public MyCustomJButton(String text, Icon icon)
    {
        super(text, icon);

        init();
    }

    public MyCustomJButton(String text)
    {
        super(text);

        init();
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {

    }

    @Override
    public void mousePressed(MouseEvent e)
    {

    }

    @Override
    public void mouseReleased(MouseEvent e)
    {

    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
        this.setCursor(handCursor);
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
        this.setCursor(defaultCursor);
    }

    private void init()
    {
        defaultCursor = this.getCursor();
        handCursor = new Cursor(Cursor.HAND_CURSOR);

        addMouseListener(this);
    }

}

, , , JButton.

MyCustomJButton myButton = new MyCustomJButton("My Button");
+1

All Articles