Mouse pointer not visually changing (Repost with more explanation)
I am having problems changing the mouse cursor. The code structure is as follows:
JFrame->JPanel->JDialog->JPanel
I am trying to change the mouse cursor from within the last JPanel (it works fine upto the JDialog level)
My code :
class ABCextends JPanelimplements MouseListener, MouseMotionListener
{
Cursor normalCursor =new Cursor(Cursor.DEFAULT_CURSOR);
Cursor handCursor =new Cursor(Cursor.HAND_CURSOR);
ABC()
{
repaint();
addMouseListener(this);
addMouseMotionListener(this);
}
publicvoid mouseExited(MouseEvent e)
{
//Change cursor
System.out.println(" "+getCursor().getType());
setCursor(normalCursor);
System.out.println(" "+getCursor().getType());
}
publicvoid mouseEntered(MouseEvent e)
{
//Change cursor
System.out.println(" "+getCursor().getType());
setCursor(handCursor);
System.out.println(" "+getCursor().getType());
}
//Paint and other mouse methods. No mouse pointer changes there
}
When I run this code, though the cursor type changes as observed by the println's, yet the pointer is not visually changed.
A sample output is as follows:
0
12
12
0
0
12
12
0
Each 0-12-12-0 represents a mouse enter and exit sequence indicating that the cursor type is actually changing but no visual effects
I'll be thankful to the person who has a solution for this.
[2302 byte] By [
coolprosua] at [2007-11-27 5:16:22]

# 1
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 2
Hi
I fixed the problem but I feel I was doing some fundamental mistake and I sincerely want to know what the mistake was. Here are the two codes aa you wanted. The first one highlights the problem I was facing and the second one is the solution I opted for.
This code was creating the problem -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NotWorking {
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(400,400);
MyPanel panel = new MyPanel();
frame.add(panel);
frame.setVisible(true);
panel.showDialog();
//This is working
//frame.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
class MyPanel extends JPanel {
MyPanel() {
//This is working
//setCursor(new Cursor(Cursor.HAND_CURSOR));
}
void showDialog()
{
MyDialog diag = new MyDialog();
diag.setModal(true);
diag.setVisible(true);
}
}
class MyDialog extends JDialog {
MyDialog() {
setSize(200,200);
//This is working
//setCursor(new Cursor(Cursor.HAND_CURSOR));
ProblemPanel panel = new ProblemPanel();
add(panel);
}
}
class ProblemPanel extends JPanel {
ProblemPanel() {
setCursor(new Cursor(Cursor.HAND_CURSOR)); //This is not working
}
}
This code is working fine -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Working {
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setSize(400,400);
MyPanel panel = new MyPanel();
frame.add(panel);
frame.setVisible(true);
panel.showDialog();
//This is working
//frame.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
class MyPanel extends JPanel {
MyPanel() {
//This is working
//setCursor(new Cursor(Cursor.HAND_CURSOR));
}
void showDialog()
{
MyDialog diag = new MyDialog();
diag.setModal(true);
diag.setVisible(true);
}
}
class MyDialog extends JDialog {
MyDialog() {
setSize(200,200);
//This is working
//setCursor(new Cursor(Cursor.HAND_CURSOR));
ProblemPanel panel = new ProblemPanel(this);
add(panel);
}
}
class ProblemPanel extends JPanel {
ProblemPanel(MyDialog diag) {
diag.setCursor(new Cursor(Cursor.HAND_CURSOR)); //This is working
}
}
I want to know that if in MyPanel (the first JPanel) the cursor is changing properly, why not in the second panel? Why do I need to call the setCursor() method of the MyDialog class? What's even more peculiar is that, as I said before, if I print the values of the cursor type, the value is changing but the cursor is not visually changing (in case of the first code)
# 3
Your NotWorking code seems to work for me. I'm using JDK 1.4.2 on XP. Here is an even simpler version that works for me as well:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Working {
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.getContentPane().setCursor(new Cursor(Cursor.HAND_CURSOR));
frame.setSize(400,400);
frame.setVisible(true);
JDialog dialog = new JDialog(frame);
dialog.getContentPane().setCursor(new Cursor(Cursor.HAND_CURSOR));
dialog.setSize(200,200);
dialog.setVisible(true);
}
}
When the dialog is open the hand displays over the dialog, but not the frame. When you close the dialog the hand displays over the frame.