Problem with using Radio Buttons in a plugin when called from a macro

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with using Radio Buttons in a plugin when called from a macro

Stein Rørvik
Hello,

I see some strange behaviour with the use of Radio Buttons in plugins.
The plugins behave correctly when called from the GUI, but not when called from a macro.

My plugin is a PlugInFilter, but the exact problem can be reproduced by the simple PlugIn example below.

The macro recorder records this, when selecting button 3:

run("Radio Buttons", "cars=Maserati");

If you run this one-line macro, the first showMessage is shown correctly but not the second one.
It is not called at all when using a macro. From the GUI it is called correctly.

Does anyone have an idea what is wrong here?

plugin code:
----------------------------------------------------------------------------------------
import ij.IJ;
import ij.gui.*;
import ij.plugin.*;

public class Radio_Buttons implements PlugIn {
                String mycar;
                String[] cars = {"Ford", "Citroen", "Maserati", "Volvo"};

                public void run(String arg) {
                               showDialog();
                               IJ.log("Cars: "+ mycar);
                               IJ.showMessage("The car you are driving is a " + mycar);
                               if (mycar == "Ford") { IJ.showMessage("That is an American car"); }
                               if (mycar == "Citroen") { IJ.showMessage("That is a French car"); }
                               if (mycar == "Maserati") { IJ.showMessage("That is an Italian car"); }
                               if (mycar == "Volvo") { IJ.showMessage("That is a Swedish car"); }
                }

                void showDialog() {
                               GenericDialog gd = new GenericDialog("Select Car");
                               gd.addRadioButtonGroup("Cars", cars, 4, 1, "Ford");
                               gd.showDialog();
                               mycar = gd.getNextRadioButton();
                }
}
----------------------------------------------------------------------------------------


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with using Radio Buttons in a plugin when called from a macro

Wayne Rasband-2
> On Apr 17, 2019, at 1:01 PM, Stein Rørvik <[hidden email]> wrote:
>
> Hello,
>
> I see some strange behaviour with the use of Radio Buttons in plugins.
> The plugins behave correctly when called from the GUI, but not when called from a macro.

You have to use equals() to compare strings in Java. For example, use mycar.equals("Ford") instead of mycar=="Ford".

-wayne

> My plugin is a PlugInFilter, but the exact problem can be reproduced by the simple PlugIn example below.
>
> The macro recorder records this, when selecting button 3:
>
> run("Radio Buttons", "cars=Maserati");
>
> If you run this one-line macro, the first showMessage is shown correctly but not the second one.
> It is not called at all when using a macro. From the GUI it is called correctly.
>
> Does anyone have an idea what is wrong here?
>
> plugin code:
> ----------------------------------------------------------------------------------------
> import ij.IJ;
> import ij.gui.*;
> import ij.plugin.*;
>
> public class Radio_Buttons implements PlugIn {
>                String mycar;
>                String[] cars = {"Ford", "Citroen", "Maserati", "Volvo"};
>
>                public void run(String arg) {
>                               showDialog();
>                               IJ.log("Cars: "+ mycar);
>                               IJ.showMessage("The car you are driving is a " + mycar);
>                               if (mycar == "Ford") { IJ.showMessage("That is an American car"); }
>                               if (mycar == "Citroen") { IJ.showMessage("That is a French car"); }
>                               if (mycar == "Maserati") { IJ.showMessage("That is an Italian car"); }
>                               if (mycar == "Volvo") { IJ.showMessage("That is a Swedish car"); }
>                }
>
>                void showDialog() {
>                               GenericDialog gd = new GenericDialog("Select Car");
>                               gd.addRadioButtonGroup("Cars", cars, 4, 1, "Ford");
>                               gd.showDialog();
>                               mycar = gd.getNextRadioButton();
>                }
> }
> ----------------------------------------------------------------------------------------

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with using Radio Buttons in a plugin when called from a macro

Stein Rørvik
Thanks, that works!

But why the difference between running from the GUI and a macro?
The comparison mycar=="Ford" works when the plugin is called via the GUI.

Stein

-----Original Message-----
From: ImageJ Interest Group <[hidden email]> On Behalf Of Wayne Rasband
Sent: 17. april 2019 20:32
To: [hidden email]
Subject: Re: Problem with using Radio Buttons in a plugin when called from a macro

> On Apr 17, 2019, at 1:01 PM, Stein Rørvik <[hidden email]> wrote:
>
> Hello,
>
> I see some strange behaviour with the use of Radio Buttons in plugins.
> The plugins behave correctly when called from the GUI, but not when called from a macro.

You have to use equals() to compare strings in Java. For example, use mycar.equals("Ford") instead of mycar=="Ford".

-wayne

> My plugin is a PlugInFilter, but the exact problem can be reproduced by the simple PlugIn example below.
>
> The macro recorder records this, when selecting button 3:
>
> run("Radio Buttons", "cars=Maserati");
>
> If you run this one-line macro, the first showMessage is shown correctly but not the second one.
> It is not called at all when using a macro. From the GUI it is called correctly.
>
> Does anyone have an idea what is wrong here?
>
> plugin code:
> ----------------------------------------------------------------------------------------
> import ij.IJ;
> import ij.gui.*;
> import ij.plugin.*;
>
> public class Radio_Buttons implements PlugIn {
>                String mycar;
>                String[] cars = {"Ford", "Citroen", "Maserati", "Volvo"};
>
>                public void run(String arg) {
>                               showDialog();
>                               IJ.log("Cars: "+ mycar);
>                               IJ.showMessage("The car you are driving is a " + mycar);
>                               if (mycar == "Ford") { IJ.showMessage("That is an American car"); }
>                               if (mycar == "Citroen") { IJ.showMessage("That is a French car"); }
>                               if (mycar == "Maserati") { IJ.showMessage("That is an Italian car"); }
>                               if (mycar == "Volvo") { IJ.showMessage("That is a Swedish car"); }
>                }
>
>                void showDialog() {
>                               GenericDialog gd = new GenericDialog("Select Car");
>                               gd.addRadioButtonGroup("Cars", cars, 4, 1, "Ford");
>                               gd.showDialog();
>                               mycar = gd.getNextRadioButton();
>                }
> }
> ----------------------------------------------------------------------------------------

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with using Radio Buttons in a plugin when called from a macro

Kenneth Sloan-2
Probably because the macro language is not Java.  In Java, "==" tests for identity.  Two objects compare as "==" if and only if they are the SAME OBJECT.  Two strings that have the same contents are not the same string.

I'm not an expert in the macro language, but I suspect that in a macro "==" test for equal VALUES, so two distinct
strings that happen to have the same sequence of characters are "==".

In Java, testing for equal values requires "equals".  Question: is there an "equals" in the macro language?

The question of "equality" vs "identity" is a tricky one.  Different languages answer it in different ways.

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On Apr 17, 2019, at 14:11, Stein Rørvik <[hidden email]> wrote:
>
> Thanks, that works!
>
> But why the difference between running from the GUI and a macro?
> The comparison mycar=="Ford" works when the plugin is called via the GUI.
>
> Stein
 
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Problem with using Radio Buttons in a plugin when called from a macro

Stein Rørvik
What baffled me was that in both cases the same compiled plugin yielded different results. How string comparison happens in the macro language is not relevant here as this happens inside the Java plugin code.

But your explanation sounds right. Since the plugin is compiled, the compiler probably assigns identical strings found in different parts of the code to one reused object to save heap space, so the comparison gets true because we are comparing the same object. When the plugin is called from the macro, the string being compared comes from the "outside" and is thus a different object, even if the value is the same. So using "equals" is the solution that will work in both cases.

Stein

-----Original Message-----
From: ImageJ Interest Group <[hidden email]> On Behalf Of Kenneth Sloan
Sent: 17. april 2019 22:27
To: [hidden email]
Subject: Re: Problem with using Radio Buttons in a plugin when called from a macro

Probably because the macro language is not Java.  In Java, "==" tests for identity.  Two objects compare as "==" if and only if they are the SAME OBJECT.  Two strings that have the same contents are not the same string.

I'm not an expert in the macro language, but I suspect that in a macro "==" test for equal VALUES, so two distinct strings that happen to have the same sequence of characters are "==".

In Java, testing for equal values requires "equals".  Question: is there an "equals" in the macro language?

The question of "equality" vs "identity" is a tricky one.  Different languages answer it in different ways.

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.





> On Apr 17, 2019, at 14:11, Stein Rørvik <[hidden email]> wrote:
>
> Thanks, that works!
>
> But why the difference between running from the GUI and a macro?
> The comparison mycar=="Ford" works when the plugin is called via the GUI.
>
> Stein
 
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html