views:

161

answers:

2

Hi,

I have a JPanel inside a dialog. A MouseListener listens to mouse movements, and when the mouse is on a specific location, I call setCursor() on the panel to change the cursor.

This all works well, untill I open another dialog from within this dialog and close it again. (For example: a warning message (JOptionPane), or a new custommade JDialog. After this action, the cursor does not change again, although I still call 'setCursor'.

Anyone an idea what happens? And how to resolve that?

+2  A: 

I tried the following and it worked fine, also after displaying another JDialog (on Windows, JDK 1.6.0_12).

Mouse cursor changes every 50 pixels in horizontal direction, clicking the JPanel opens a modal JDialog. Close it again and mouse cursor still changes.

public class DialogCursorTest extends JDialog{
    public DialogCursorTest() {
        final JPanel panel = new JPanel();
        panel.addMouseMotionListener(new MouseMotionAdapter() {
            Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
            @Override
            public void mouseMoved(MouseEvent e) {
                if(e.getX() % 100 > 50) {
                    if(panel.getCursor() != handCursor) {
                        panel.setCursor(handCursor);
                    }
                }
                else {
                    if(panel.getCursor() == handCursor) {
                        panel.setCursor(Cursor.getDefaultCursor());
                    }
                }
            }
        });

        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new JDialog(DialogCursorTest.this, "Test", true).setVisible(true);
            }
        });

        getContentPane().add(panel);
    }

    public static void main(String[] args) {
        DialogCursorTest test = new DialogCursorTest();
        test.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        test.setSize(400, 300);
        test.setVisible(true);
    }
}
Peter Lang
Works for me too, I will test it this evening on my mac. And try to find out what's the difference :(
Fortega
A: 

I found the solution: problem was I had 1 frame and 1 dialog. The frame is the main frame, the dialog is created afterwards. From the dialog, I call new JDialog(null, "title"); In stead of using null, I should have added the calling dialog, because after closing the dialog, the focus went to the main frame, although on my mac it looked like the focus was on the dialog...

Fortega