Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
setCursor bug in JFrame
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

setCursor bug in JFrame



I just submitted this as bug # 3746741. Here it is in case anyone is interested:

30-Jul-2004 02:30 PM Robert Ross:
After explicitly setting a cursor on a JFrame (via Component.setCursor()), the mouse pointer/cursor does not behave as expected. I've enclosed a simple test class. It's in the default package, so compile via "javac TestSetCursor.java", and run via "java TestSetCursor".

I expect that when I move the mouse over my JFrame, the cursor should change to my custom cursor. (In my example I'm using a crosshair cursor.) When I move the mouse OUTSIDE the JFrame, over another app for example, I expect the mouse cursor to change to a pointer, or whatever is the right cursor for that app. For example, if I move the mouse so that the cursor is over my Finder desktop, I expect the cursor to change to a pointer - but it does not, it stays as a crosshair.

If I then click the mouse in the menu bar or finder desktop, the mouse cursor changes to a pointer. But if I move the mouse back over my JFrame, it stays as the pointer cursor, and does not change back to the crosshair cursor. You can see that the JFrame still has the cursor set to be the crosshair cursor by the message I paint into to frame's panel. If I press the space bar, the test app sets the cursor on the JFrame to null, and if I press again, back to a crosshair. At this point, with the mouse over the JFrame, the correct cursor is rendered.

It seems that the JFrame doesn't change the cursor when the mouse enters/leaves it as it should.

Note that this behavior does work as expected in the Sun JRE 1.4.2_04 on WindowsXP (yes I understand they're different platforms,versions, etc, but just to mention it for comparison).

This is my first bug report, so hopefully I've included everything you need.

Thanks!

Rob

//------------ File TestSetCursor.java

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
* JFrame Template
*
* @author Rob Ross
* @version Jul 29, 2004 9:38:30 PM
*/

public class TestSetCursor extends JFrame
{

static Cursor customCursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
public TestSetCursor()
{
init();
}

private void init()
{
getContentPane().setLayout(new BorderLayout());
setSize(400, 300);
setTitle("TestSetCursor");
this.setLocation(100, 100);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
super.windowClosing(e);
System.exit(0);
}

});
this.addKeyListener(new KeyAdapter(){

// from the KeyListener interface
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
// exit the program
System.exit(0);
}
else if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
// change cursor
changeCursor(); //only runs in AWT thread
}
}
});
}

public static void runApp()
{
JFrame myFrame = new TestSetCursor();
myFrame.setVisible(true);
myFrame.setCursor(customCursor);
}

public static void main(String[] args)
{
Runnable r = new Runnable(){

public void run()
{
runApp();
}
};
SwingUtilities.invokeLater(r);//run in AWT event thread
}

/**
* This method is only called from the AWT thread
*/
public void changeCursor()
{
Cursor c = getCursor();
if (c == customCursor)
{
setCursor(null);
}
else
{
setCursor(customCursor);
}
repaint();
}

public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.BLACK);
String name = getCursor().getName();

g.drawString("Current JFrame cursor is "+name,5,40);
g.drawString("Press Space to change mouse cursor.", 5,60);
g.drawString("Press Escape to exit.", 5, 80);
}
}

//--------------

Rob Ross, Senior Software Engineer
E! Networks
rross @t eentertainment.com
---------------------------------------------------
"Beware of he who would deny you access to information, for in his heart he dreams himself your master." -- Commissioner Pravin Lal
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.