Adding a GUI to your macro

Sometimes you may want to try different values for some parameters without having to edit the macro code again and again in order to change the hardcoded variables. The Dialog functions allow you to build your own user interfaces to query the user for information before running the macro. A dialog is built by calling Dialog.create(title) and then adding the different elements that compose the user interface: textboxes for strings/numbers, sliders, selection menus, checkboxes, and radio buttons. Once all the elements are in place, we just need to get the user input and proceed with the processing of the macro.

The following code creates a simple dialog and will allow us to explore the possibilities of the dialog building functions with some additional nicety:

// We are using some relatively new functions, so we have to
// explicitly forbid this macro from running in old ImageJ versions
requires("1.47r");
// Create the main window with its title
Dialog.create("This is a test dialog");
// Grab two strings using a box 20 characters wide
Dialog.addString("String 1:", "First string", 20);
Dialog.addString("String 2:", "Second string", 20);
// And a number
Dialog.addNumber("Number:", 0);
// And a slider from 0 to 10, centered in 3
Dialog.addSlider("Slider:", 0, 10, 3);
// And a checkbox (checked)
Dialog.addCheckbox("Checkbox", true);
// And a radio button group with 3 rows and 2 columns,
// mark "2.0" as the default option
rboptions = newArray(1, 2, 3, 4, 5, 6);
Dialog.addRadioButtonGroup("Radio buttons", rboptions, 3, 2, "2.0");
// Finally, show the dialog on screen
Dialog.show();
// We are done building, now we need to get the values when the user clicks OK.
// We use the Dialog.get functions in the same order used
// to place the elements on the dialog.
s1 = Dialog.getString();
s2 = Dialog.getString();
n1 = Dialog.getNumber();
slider1 = Dialog.getNumber();
c1 = Dialog.getCheckbox();
b1 = Dialog.getRadioButton();
// Print all
print(s1, s2, n1, slider1, c1, b1);

When you run this macro, you will get a dialog similar to the following one, if everything went right:

Adding a GUI to your macro

If the user clicks on Cancel, the window closes and the macro will not be executed. If the user clicks on OK, the variables are assigned and the macro continues its execution. In this case, it just prints the values on the screen one after the other.

Note the first line of the macro: the requires(version) function allows the macro to be run in versions newer than (and including) the one provided as an argument. It is a nice way of notifying the user, the need for updating the version before running a macro that may potentially not work in older versions.

Tip

If you only need to get one number or one string, you can skip most of these steps and simply call one of these special functions: getNumber(message,defaultvalue), getString(message,defaultvalue), where message is the text the user will see on the screen and defaultvalue is the initial value in the textbox. You do not need to call Dialog.create(title) before running any of these functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset