GUI related question & request

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

GUI related question & request

Fred Damen

First the request.  The GenericDialog/NonblockingGenericDialog wants to
assign an ImageWindow as its parent. One of the effects of this is that
this ImageWindow is raise(d) when the GUI is activated.  For the
GenericDialog this is not an issue as the GUI is always active, until it
is gone, AND, there is an Java API to assign a benign Frame instead of the
current ImageWindow. For the NonblockingGenericDialog this ImageWindow is
raised everytime the GUI is raised and there is no API to assign a benign
alternative. Could there be an NonblockingGenericDialog API to allow the
Java programmer to set a benign Frame as its parent?

Second the question.  I am trying to get screen capture of a GUI window
using Robot. Everthing works fine IF the window to be captured does not
lose focus after the GenericDialog instagating the screen capture closes
and Robot captures the screen.  If the window to be captured loses focus,
the screen that gets captured is the one that was current at the moment
the OK button was clicked; this is even though the screen has been
properly updated before the Robot.createScreenCapture. Everything that I
have tried has not changed this scenerio. Note that this works the same
with ImageWindow(s).  The reason I am concerened is that a win.toFront is
done before the screen capture.
1) Has anyone experienced this exact issue?, and if so, how to fix?
2) Is there a way to prevent a focus change between the toFront and
3) Is there some sort of double buffering going on that can be requested
to update so what is captured is current?

The below plugin can be used to reproduce the aformentioned issue.
Although it depends on GUI interaction timing and refocusing, so it may
take a few tries to reproduce.  Have the below GUI, or something else,
cover the to-be-captured window before clicking OK.

Thanks in advance,


import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.util.*;
import ij.plugin.*;
import ij.plugin.frame.*;

public class Find_Window implements PlugIn {

   public void run(String arg) {
      Vector<String> itv = new Vector<String>();
      int[] ids = WindowManager.getIDList();
      if (ids != null)
         for(int id : ids)

      Vector<String> wtv = new Vector<String>();
      Window[] ws = WindowManager.getAllNonImageWindows();
      if (ws != null)
         for(Window w : ws)
            wtv.add(w instanceof Frame ? ((Frame)w).getTitle() :

      GenericDialog gd = new GenericDialog("Find Windows");
      gd.addChoice("Find Image:", itv.toArray(new String[0]), "");
      gd.addMessage("Applies only to ImageWindow(s)");
      gd.addMessage("  ");
      gd.addChoice("Find Frame:", wtv.toArray(new String[0]), "");
      gd.addMessage("Applies to all Window(s)\n ");
      if (gd.wasCanceled()) return;

      int iind = gd.getNextChoiceIndex();
      boolean unlock = gd.getNextBoolean();
      boolean here   = gd.getNextBoolean();
      boolean raise  = gd.getNextBoolean();
      boolean zoom   = gd.getNextBoolean();
      boolean snap   = gd.getNextBoolean();
      if (iind > 0) {
         ImagePlus imp = WindowManager.getImage(ids[iind-1]);
         if (unlock) imp.unlock();
         ImageWindow win = imp.getWindow();
         if (here)   win.setLocation(gd.getLocation());
         if (zoom)   win.getCanvas().zoom100Percent();
         if (raise)  win.toFront();
         if (snap)   { IJ.wait(250); snapAndDisplay(win); }

      int wind = gd.getNextChoiceIndex();
      if (wind > 0) {
         Window win = ws[wind-1];
         if (here)   win.setLocation(gd.getLocation());
         if (zoom)
            if (win instanceof GenericDialog) ((GenericDialog)win).pack();
                                         else win.setSize(500,500);
         if (raise)  win.toFront();
         if (snap)   { IJ.wait(1000); snapAndDisplay(win); }

   public static void snapAndDisplay(Window win) {
      ImagePlus imp = snap(win);
      if (imp == null) return;;
   public static ImagePlus snap(Window win) {
      try {
         Rectangle r = new Rectangle(win.getLocation(),win.getSize());
         //new Robot().mouseMove(win.getX()+10,win.getY()+10);
         new Robot().waitForIdle();
         //boolean flag = win.isAlwaysOnTop();
         ImagePlus imp = new ImagePlus("Snapshot",new
         return imp;
      catch(Exception e) {
         IJ.showStatus("Could not grab snapshot of window");
      return null;
   public static void clipboard(final ImagePlus imp) {
         new Transferable() {
            @Override public DataFlavor[] getTransferDataFlavors() {
                      return new DataFlavor[] { DataFlavor.imageFlavor };
            @Override public boolean isDataFlavorSupported(DataFlavor
flavor) {
                      return DataFlavor.imageFlavor.equals(flavor);
            @Override public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
                      if (!isDataFlavorSupported(flavor))
                         throw new UnsupportedFlavorException(flavor);
                      return imp.getImage();
            }, null);

ImageJ mailing list: