Mouse Listener in ImageJ

Hi,
I want to add a MouseListener to my ImageJ plugin. But my code does not work.

public class MouseClick implements PlugIn, MouseListener
{

    @Override
    public void mouseClicked(MouseEvent e) {
        IJ.showMessage("Mouse clicked");
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void run(String arg0) 
    {
        IJ.open();
        ImagePlus imp = WindowManager.getCurrentImage();
        imp.getWindow().addMouseListener(this);
    }
    
}

How can I make it working?

re342

2 Likes

EDIT by @re342: With replacing getWindow() by getCanvas(), it works:

public class MouseClick implements PlugIn, MouseListener
{

    @Override
    public void mouseClicked(MouseEvent e) {
        IJ.showMessage("Mouse clicked");
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void run(String arg0) 
    {
        IJ.open();
        ImagePlus imp = WindowManager.getCurrentImage();
        imp.getCanvas().addMouseListener(this);
    }
    
}
1 Like