Search code examples
javaswingjbuttonmousemouselistener

setCursor Method not Working on one MouseListener (Java)


I have a MouseListener for a button that basically says when the mouse enters the vicinity of that button, it changes the cursor to the hand cursor. All my mouse listeners are working fine, but for some reason, this one single mouse listener isn't working. It is identical to all other MouseListeners.

viewAssignments.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
        // When mouse enters vicinity of a button, sets cursor to hand cursor.
        viewAssignments.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public void mouseExited(MouseEvent e) {
        // When mouse exits vicinity of the button, sets cursor to default cursor.
        viewAssignments.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
});

Here is the single one that is not working:

about.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
        // When mouse enters vicinity of a button, sets cursor to hand cursor.
        about.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public void mouseExited(MouseEvent e) {
        // When mouse exits vicinity of the button, sets cursor to default cursor.
        about.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
});

I am not sure why this one isn't working.


Solution

  • I just called setCursor method. I didn't need to add a mouse adapter anyway. Thank you VGR.