Posted by
Wayne Rasband on
URL: http://imagej.273.s1.nabble.com/Changing-location-of-a-generic-dialog-tp3694061p3694067.html
On Jan 13, 2009, at 2:06 PM, Curtis Rueden wrote:
> 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
The 1.42h daily build adds a centerDialog(boolean) method to
GenericDialog that can be used to prevent the GUI.center() call, and
thus prevent the flashing that occurs when the dialog is moved. You
should also test to see which version of ImageJ your plugin is running
on to avoid NoSuchMethodError exceptions, for example:
if (IJ.getVersion().compareTo("1.42h")>=0)
gd.centerDialog(false);
-wayne
-----
// 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);
if (IJ.getVersion().compareTo("1.42h")>=0)
gd.centerDialog(false);
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);
}
}