Search code examples
javasingletonimagej

Open new images in ImageJ always in the same window programatically (java)


I am using imageJ to extend some functionality of a Swing application

I want to open an image with ImageJ using java, but i want to keep the same image window and not a new window per each image opened..

ImagePlus imp = IJ.openImage(imageFilepath)  
imp.show()  

also tried something like that but could not figure it out from the documentation..

   ImagePlus imp  
   imp = IJ.openImage(imageFile.absolutePath)  
   if (imp) {  
        imp.setImage(imp)  
   }  
   imp.show()  

Solution

  • Readin the doc you should do something like this:

    class YourClass {
        ImagePlus imp;
    
        public void openImage( String imageFilepath ) {
            if ( imp == null {
                imp = IJ.openImage(imageFilepath);  
            }
            else {
               // Reuse
               ImagePlus imp2 = IJ.openImage(imageFilepath);
               imp.setImage(imp2) 
            }
            imp.show();  
        }
    }