Search code examples
javavectorimagej

ImageJ: Get specific Radiobox


I'm working on a Plugin for compressing images. Before the "magic happens" the user should decide, if he wants use a method with three or four neighbors. For this, I created a generic Dialog with a RadioButtonGroup.

This works fine, my question is how do I get the choice of the user? The Method getRadioButton gives a Vector back. But I don’t know how to handle this. My plan was to use the Index of the Button which was choose as a parameter for my main class, but I don’t see a way to manage this.

Have you any idea?

Edit: My Code (without import)

public class FrameDemo_ extends PlugInFrame {
    public FrameDemo_ (){
        super("FrameDemo");
    }

    public void run (String arg){
        String[] items = {"Option A", "Option B"};
        GenericDialog gd = new GenericDialog("FrameDemo settings");
        gd.addRadioButtonGroup("Test",items,2,1,"0");
        gd.showDialog();
        if (gd.wasCanceled()){
            IJ.error("PlugIn canceled!");
            return;
        }
        String input;
        Vector vec = gd.getRadioButtonGroups();
        Object obj = vec.elementAt(0);
        input = obj.toString();

        this.setSize(250, 250);
        this.add(new Label(input, Label.CENTER));
        this.setVisible(true);
    }
}

My Target is to do something like:

input = obj.Somefunction; // Input contains for exampe "A" for Option A

class RealPlugin (parameter input){
    if(input == A) { do something } // Pseudocode...not the real if for a string 

else if {input == B) {do something else }

My Problem is, when I convert the Object to String it is:

java.awt.CheckboxGroup[selectedCheckbox=java.awt.Checkbox[checkbox0,0,0,66x23,invalid,label=Option A,state=true}}

I'm sure there is a way for String manipulation, but I don't think this is the proper way to do this. I mean this must be a so typical job (use a radiobuttongroup to get a user choice) that there must be a clever way or a function...


Solution

  • Note: questions like these are usually answered much faster on the ImageJ forum, where more ImageJ experts will read them.


    To retrieve the result from a radio button group, use the getNextRadioButton method of GenericDialog. Here's a small Groovy script that can be run directly from the script editor in ImageJ:

    import ij.gui.GenericDialog
    
    gd = new GenericDialog("FrameDemo Settings")
    
    items = ["Option A", "Option B"]
    gd.addRadioButtonGroup("Test", (String[]) items, 2, 1, "0")
    
    gd.showDialog()
    
    if (gd.wasOKed()) {
        answer = gd.getNextRadioButton()
        println answer
    }
    

    With ImageJ2 (that comes included with the Fiji distribution of ImageJ), it's much easier to get this kind of choice, using SciJava script parameters:

    // @String(label="Choice", choices={"Option A", "Option B"}, style="radioButtonVertical") choice
    
    println choice
    

    In Java, the same would look like this:

    @Plugin(type = Command.class, menuPath = "Plugins>My New Plugin")
    public class MyNewPlugin implements Command {
    
        @Parameter (label="Choice", choices={"Option A", "Option B"}, style="radioButtonVertical") 
        private String choice;
    
        // your code here
    }
    

    For a full example, see https://github.com/imagej/example-imagej-command