Reputation: 49
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.
Upvotes: 1
Views: 701
Reputation: 324207
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.
Upvotes: 4