Login  Register

Re: Changing location of a generic dialog

Posted by ctrueden on Jan 13, 2009; 7:06pm
URL: http://imagej.273.s1.nabble.com/Changing-location-of-a-generic-dialog-tp3694061p3694066.html

Hi Jon,

My plugin uses a generic dialog, but it pops up in an inconvenient place.
>  Is there any way I can change the location?  Maybe somehow place it the
> second time in the same place the user moved it?
>

From what I can tell, GenericDialog.showDialog() calls GUI.center(this) and
then blocks, which doesn't leave much wiggle room for moving the dialog
around. Maybe Wayne will have a slicker solution, but one thing you can do
is react to a windowOpened event to immediately move the dialog as soon as
it pops up. The downside is that the dialog will momentarily flash in the
wrong place. See the plugin below for an example.

HTH,
Curtis

-----
// Place_Dialog.java

import ij.IJ;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Place_Dialog implements PlugIn, WindowListener {
  private GenericDialog gd;

  public void run(String arg) {
    // construct generic dialog
    gd = new GenericDialog("Who Are You?");
    gd.addStringField("Name: ", "", 30);
    gd.addWindowListener(this);
    gd.showDialog();
    if (gd.wasCanceled()) return;

    // do something trivial with the input
    String name = gd.getNextString();
    if (name == null) name = "";
    name = name.trim();
    if (name.equals("")) IJ.showMessage("Hello!");
    else IJ.showMessage("Hello, " + name + "!");
  }

  public void windowActivated(WindowEvent e) { }
  public void windowClosed(WindowEvent e) { }
  public void windowClosing(WindowEvent e) { }
  public void windowDeactivated(WindowEvent e) { }
  public void windowDeiconified(WindowEvent e) { }
  public void windowIconified(WindowEvent e) { }
  public void windowOpened(WindowEvent e) {
    gd.setLocation(10, 10);
  }
}

On Tue, Jan 13, 2009 at 12:24 PM, Jon Harman <[hidden email]>wrote:

> Hi,
> My plugin uses a generic dialog, but it pops up in an inconvenient place.
>  Is there any way I can change the location?  Maybe somehow place it the
> second time in the same place the user moved it?
>
> Jon
>