1

Okay so, I have this function which I call on every JButton I create, and it works fine.

public void addcursor(JButton button)
{
    button.getModel().addChangeListener(new ChangeListener(){
        public void stateChanged(ChangeEvent e){
            ButtonModel model=(ButtonModel) e.getSource();
            if(model.isRollover())                  
                setCursor(new Cursor(Cursor.HAND_CURSOR));              
            else
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

        }
    });
}

However, this code works only when I move over a JButton, and sets the mouse cursor back to default when I move away from the Button. So, on a separate class/function:

gui.getRootPane().setCursor(new Cursor(Cursor.WAIT_CURSOR));
gui.getRootPane().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

After calling those 2 functions, the first function addcursor(JButton) doesn't work anymore, I just want to set the buttons getModel back to how it was, after setting the cursor back to default. Note that I also tried re-calling the addcursoor(JButton) function after setting the crusor to default, but it still didn't work. Thank you.

2
  • Why can't you just set cursors directly to the jbuttons? Why monitor its model, and changing the global cursor? This may cause your conflicting cursor setting problem. Commented Oct 31, 2016 at 20:01
  • I'm unclear as to what you are trying to acheive Commented Oct 31, 2016 at 20:07

1 Answer 1

4

Components already support a cursor that will change on a mouse entered event:

button.setCursor( Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

The cursor will also reset on a mouse exited event. So you don't need any special logic to support this type of functionality.

setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

I've always just used setCursor(null) when doing manual manipulation of the cursor.

Sign up to request clarification or add additional context in comments.

1 Comment

Much easier than the whole function that I was using, thanks alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.