How to change input in GenericDialog

imagej

#1

I have few question:

First, I have a file blabla.class (it’s a custom generic dialog) how can I use it in a macro?

Secondly for exemple I had a choice box and I wanted to change the input during the running of the plugin how can I do it?

I have my class that implements the interface DialogListener and I have the method DialogItemChanged but I don’t know how I can change the input after being initializing it.

I search for exemple code and on the doc, there are no clues for me :frowning:


#2

Good day!

First, I have a file blabla.class (it’s a custom generic dialog) how can I use it in a macro?

If you code plugins that are supposed to be macro-recordable you can’t use dialogs other than “Generic Dialogs”.

From a macro you call such plugins by:

run(“command”[, “options”])
Executes an ImageJ menu command. The optional second argument contains values that are automatically entered into dialog boxes (must be GenericDialog or OpenDialog). Use the Command Recorder (Plugins>Macros>Record) to generate run() function calls. Use string concatentation to pass a variable as an argument. With ImageJ 1.43 and later, variables can be passed without using string concatenation by adding “&” to the variable name. For examples, see the ArgumentPassingDemo macro.

Regarding ImageJ macro coding please consult:
https://imagej.nih.gov/ij/developer/macro/functions.html

Secondly for exemple I had a choice box and I wanted to change the input during the running of the plugin how can I do it?

You need a callback function in Java or in one of the higher-level scripting languages.

As mentioned before, some of your questions are essentially Java-related and have no direct relation to ImageJ oder image processing.

Regards

Herbie


#3

Indeed this time I will ask elsewhere but thanks anyway :slight_smile:


#4

I have a mini question because I feel that you know your stuff, when using run(“command”, “option”);
Do I use the “option” to pass argument to my plugins ?

Blockquote
public void run(String arg) {
if (IJ.versionLessThan(“1.31l”)) {
return;
}
// permet de récupérer CountResult de la macro
if (!arg.equals("")) {
gridHeight = Integer.parseInt(arg);
}
if (showDialog()) {
displayValues();
}
}

How I can pass pass something as “arg” in the code.
For exemple doing : run(“name of the plugins”, “arg=3”); doesn’t work at all.


#5

Could you please try to tell us in a more detailed way what you are trying to do?

Regards

Herbie


#6

1.1) I’m trying to make a generic dialog customized (in java, so it’s a plugin) and I wanted to use it in macro but I realize that first I need to give input like how much it needs to create line of inputs in my plugin so I don’t know how to pass input to my plugin via run(“my plugin” ,"option);

1.2) And I also realize that I don’t know how to get the ouput from the plugin after the run(“my plugin”, “option”);

  1. So I think, I just need to construct a generic dialog directly in the macro so I can access the input I need to give to the generic dialog and I can access the output of the generic dialog directly also but it’s kinda limited me because for exemple in java I can do like gd.addPanel but I don’t see this function in macro.

But anyway I will go for the 2) option if I can’t resolve problem 1.1) & 1.2)


#7

Dear,

I think things are much easier than you expect them to be.

Regarding plugin-coding please study carefully the tutorial that is given here:
https://imagingbook.files.wordpress.com/2013/06/tutorial171.pdf

If you use a generic dialog in the plugin then you call this plugin from a macro by using the run command mentioned earlier. The options string must contain all variables that you normally enter to the generic dialog of the plugin.

Please start with a simple example that requires only one variable and test it.

Good luck

Herbie


#8

OKay but how about get the output of the plugin, I did look at the tutorial there’s no where, where it said how I can retrieve it.

And I still don’t get it how, I try

Blockquote
public class Dialog_Test implements PlugIn {

int go= 0;

public void run(String arg) {
    if (IJ.versionLessThan("1.31l")) {
        return;
    }
   if(go == 1)
        ShowDialog();

}

with run(“Dialog Test”, “go=1”) in the macro

Sorry to bothering you so much but that’s quite hard to get it into it, I think it lacks so much documentations or else I don’t know how to find them

Sadly Strife


#9

What I do to return data from a plugin (named e.g. My_Plugin) is to concatenate the values in a string and separate the values by whatever symbol (blank, semicolon, comma …)

In case of a single value, I write in the run-method for instance:

resultString = "" + myValue; // return string for macro calls

where “resultString” is a global e.g. defined before as:

static String resultString; // macro return string

Then I have a method in my plugin code:

public static String macroReturn( ) {
		return resultString;
}

In a macro I get the return string by calling the very method:

myResult = call( “My_Plugin.macroReturn” );
print( myResult );

HTH

Herbie


#10

Ohhh thank you that’s life savior really! Espacially call(“My_Plugin.function”); I have a way to get result :slight_smile: . Now back to square one and that’s my last problem that is how to give input to the plugin via run(“My_Plugin”, “option”); because I still didn’t figure out a way of making it works.
(In fact I didn’t get which parameter you’re talking)

Do you have a little exemple so I understand ?

Sorry and definitely thank you!


#11

Dear,

obviously you haven’t yet understood the idea of “Generic Dialog” and please accept that the following only works with a “Generic Dialog”!

In the run-method of your plugin( named e.g. My_Plugin) you should have this line:

if ( !getParameters() ) { return; }

Then you need a method that organizes and displays the “Generic Dialog”:

boolean getParameters() {
		GenericDialog param = new GenericDialog( "My Plugin Param", IJ.getInstance() );
		param.addNumericField( "My_Value", x, 0 );
		param.showDialog();
		if( !param.wasCanceled() ) {
			x  = (int)param.getNextNumber();
			return true;
		} else
			return false;
} 

In the above example this dialog asks for a single integer number entry.

The integer number variable x is defined as a global:

private static int x = 0;

If you run your plugin directly (not called from a macro), then the dialog will appear as desired.

If you call your plugin from a macro and want to pass an integer value to variable x, you need the macro code:

run( "My Plugin", "my_value=12345" );

If your “Generic Dialog” asks for more than one entry, like in this snippet

// …
    param.addNumericField( "My_Value1", x, 0 );
    param.addNumericField( "My_Value2", y, 0 );
    param.showDialog();
    if( !param.wasCanceled() ) {
          x  = (int)param.getNextNumber();
          y  = (int)param.getNextNumber();
// …

then you must separate the entries in the macro string by blanks like for instance:

run( "My Plugin", "my_value1=12345 my_value2=98765" );

HTH

Herbie


#12

I understand now but it’s like I want to pass a parameter that is independant from the generic dialog through “arg” that is in void run(String arg) like

Blockquote
void run(String arg) {
if(!arg.equals("’)) {
numberOfInput = Integer.parseInt(arg);
}
// blabla…
}

Where numberOfInput let me know how much line of input I have to create in my genericDialog.

EDIT :
Or maybe I think of a workaround like I have a variable static like :
Static int howMuchLine;

I have a setter
void setHowMuchLine(int);

and I would use call(“My_plugin.setHowMuchLine”, 4); for exemple and I will use this variable to init the good number of line in the generic dialog, I think it will work probably

Cordialy Strife


#13

Oh well,

you may attach string items to your liking and please understand that the parameter “arg” is not involved.

I think it will work probably

This isn’t a good way of proceeding …

What I do to pass data beyond that needed for the “Generic Dialog” is the following:

In method

public int setup( String args, ImagePlus imp ) {
		options = new String[6];
		String opt = Macro.getOptions();
		if ( ( opt != null ) && opt.contains( ";" ) ) {
			options = Tools.split( opt, ";" );
		}
                // etc
}

with a global string array defined as:

private String[] options = null; //{ "", "", "", "", "", "" };

I declare the number of string items (here 6) and a seperator char (here “;”).

You could have found all that information by searching the web and I hope you do so from now on.

Regards

Herbie


#14

Hello,

Sorry once again but with the following code :

Blockquote
public class Dialog_Grid implements PlugInFilter {

private String[] options = null;

public void run(ImageProcessor ip) {
    if (IJ.versionLessThan("1.31l")) {
        return;
    }
    showDialog();
}

boolean showDialog() {
    gd = new GenericDialog("Selection type");
    
    for (int i = 0; i < gridHeight; i++) {
        gd.setInsets(0, -70, 0);
        gd.addChoice(Integer.toString(i + 1), tab, tab[1]);
    }
    
    double recoil = Math.floor(-22.5 * gridHeight);
    gd.setInsets((int) recoil, 90, 0);
    gd.addPanel(makePanel(gd));
    gd.showDialog();
    if (gd.wasCanceled()) {
        return false;
    }
    return true;
}
@Override
public int setup(String string, ImagePlus ip) {
    options = new String[6];
    String opt = Macro.getOptions();
    if ((opt != null) && opt.contains(";")) {
        options = Tools.split(opt, ";");
    }
    return NO_IMAGE_REQUIRED;
}

}

I made the code shorter as possible.

With this plugin when I’m trying to call it in my macro with “run(‘Dialog Grid’);” the dialog is showing but in the case "run(‘Dialog Grid’, ‘option1;option2’); it doesn’t work anymore and the dialog doesn’t show up.

And I don’t understand why.


#15

If you call a plugin with a “Generic Dialog” from a macro, the plugin expects the dialog parameters in the correct sequence from the macro with optional parameters appended.

I’ve never tried to call a plugin with optional parameters only.

EDIT:
Just tried it and it works perfectly:
Make sure you start the options string by " ;option1;option2;etc"

Regards

Herbie