views:

2119

answers:

2

I have a Java Swing application, developed on Mac OS X 10.5 using Java 1.5.

I'm trying to make a custom cursor appear when the user moves the mouse over some text in a dialog. The cursor never changes, though.

When I don't use a JFrame instead of a JDialog, the cursor does change. But then I'll have to write all the dialog code myself.

How can I get the cursor to appear?

Here's the simplest code I could create to demonstrate the problem:

import javax.swing.*;
import java.awt.*;

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JDialog dialog = pane.createDialog(null, "Test Dialog");
        dialog.setVisible(true);
    }
}
+1  A: 

Looks like it is a bug in Java 1.5: I first tried with Java 1.6.0_07 and it worked as expected (on Windows XP). Then I recompiled with Java 1.5.0_06 and indeed the cursor remains in default state.

Knowing the difficulties of Java 1.6 on MacOS, I see it will be hard to fix that...

Bug ID: 5079694 JDialog doesn't respect setCursor
They give a workaround...

[EDIT] Tested workaround:

public class CursorTest extends JFrame
{
  private CursorTest()
  {
  }

  private void ShowDialog()
  {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[] { "OK" } );

        JDialog dialog = pane.createDialog(this, "Test Dialog");
        dialog.setVisible(true);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        CursorTest testFrame = new CursorTest();
        testFrame.setTitle("Test GUI");
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setSize(500, 300);
        testFrame.setVisible(true);
        testFrame.ShowDialog();
      }
    });
  }
}

Works fine with my JDK & system.

PhiLho
A: 

Thanks PhiLho, that Sun bug report gave me the solution. The owner (parent frame) must be non-null and showing. For the record, here's a modified version of my example code that does show a hand cursor.

import javax.swing.*;
import java.awt.*;

public class CursorTest {

    public static void main(String[] args) {
        JLabel label = new JLabel("Move mouse here for hand cursor");
        label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        JOptionPane pane = new JOptionPane(label);
        pane.setOptions(new Object[]{"OK"});

        JFrame parent = new JFrame();
        parent.setVisible(true);
        JDialog dialog = pane.createDialog(parent, "Test Dialog");
        dialog.setModal(false);
        dialog.setVisible(true);
    }
}
Steve McLeod