Thanks Michael,

I too looked at the code and agree with you on what is happening.

Although the issue tripped me up for some time, there is an easy work
around for when you ImagePlus close events but do not want the ImageJ
toolbar visible.

Just create the ImageJ instance with the NO_SHOW argument.

'''
   new ImageJ(ImageJ.NO_SHOW);
'''

Regards to all -- Michael Ellis

On Tue, 13 May 2025 at 23:35, Michael Schmid <[email protected]>
wrote:

> Hi Michael,
>
> no sure whether it is intended, but this is what happens:
>
> Closing an ImageWindow calls ImageWindow.windowClosing (line 619).
>
> If there is an instance of ImageJ, this runs "Close"
> (Commands.closeimage), which calls imp.close(), where imp is the
> ImagePlus. This calls imp.flush(), which activates the 'imageClosed'
> callback of an ImageListener.
>
> If there is no instance of ImageJ, ImageWindow.windowClosing only runs
> 'dispose' (of the GUI elements associated with it), but not imp.flush.
> In a mode without an ImageJ panel (using ImageJ as a library), it might
> be intended that closing the image does not call 'flush', which would
> also delete the ImageProcessor/ImageStack arrays.
>
> Not calling the 'imageClosed' callback of an ImageListener might be a
> side effect that no one thought about.
>
> But this is only speculations; I have no real insight in how ImageJ
> should work without the ImageJ panel.
>
>
> Michael
> ________________________________________________________________
> On 12.05.25 14:14, Michael Ellis wrote:
> > I am not sure if this is a bug or a feature, but it caused me some head
> > scratching.
> >
> > An imageListener registered with ImagePlus.addImageListener() does NOT
> get
> > called when an ImagePlus is closed unless an instance of an ImageJ has
> been
> > created. This is not the behaviour for imageOpened() and ImageUpdate()
> > which do get called.
> >
> > === SSC ===
> >
> > package biz.dsuk.testimagejimpclose;
> >
> > import ij.ImageJ;
> > import ij.ImageListener;
> > import ij.ImagePlus;
> > import ij.process.ByteProcessor;
> >
> > public class TestImageJImpClose {
> >
> >      public static void main(String[] args) {
> >
> >          ImagePlus.addImageListener(new ImageListener() {
> >              @Override
> >              public void imageOpened(ImagePlus ip) {
> >                  System.out.printf("Opened image%n");
> >              }
> >
> >              @Override
> >              public void imageClosed(ImagePlus ip) {
> >                  System.out.printf("Closed image%n");
> >              }
> >
> >              @Override
> >              public void imageUpdated(ImagePlus ip) {
> >                  System.out.printf("Updated image%n");
> >              }
> >          });
> >
> >          /**
> >           * When an ImageJ instance is not created, our image listener
> is NOT
> >           * called when the image is closed. Run this and close the
> "Image"
> > window
> >           * by clicking on its close icon.
> >           */
> >          //t imageJ = new ImageJ();
> >          ByteProcessor bp = new ByteProcessor(256, 256);
> >          ImagePlus imp = new ImagePlus("Image", bp);
> >          imp.show();
> >      }
> > }
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Reply via email to