Login  Register

Re: Macros: change Dialog checkbox state without clicking it?

Posted by Bill Christens-Barry-2 on Sep 18, 2017; 10:15pm
URL: http://imagej.273.s1.nabble.com/Macros-change-Dialog-checkbox-state-without-clicking-it-tp5019373p5019386.html

Curtis, Wayne,

Thanks for pointing me to a solution. For the moment I'm going with a JavaScript approach since I have much to learn about ImageJ2. This code allows me to set or clear individual or all checkboxes in a CheckboxGroup, based on two other control checkboxes:

    listener = new DialogListener() {
        dialogItemChanged : function(gd, event) {
            doAllState = gd.getNextBoolean();
            doNoneState = gd.getNextBoolean();
            allBoxes = gd.getCheckboxes();
            for (i = 2; i < 22; i++) {
                thisBox = allBoxes.get(i);
                if (doAllState)
                    thisBox.setState(true);
                if (doNoneState)
                    thisBox.setState(false);
            }
    doAll = gd.getCheckboxes().get(0);
    doAll.setState(false);
    doNone = gd.getCheckboxes().get(1);
    doNone.setState(false);
           return true;
        }
    };

    var labels = [];
    var defaults = [];
    for (i = 0; i < 20; i++) {
        labels[i] = "✓box "+(i+1);
        defaults[i] = false;
    }
    gd = new GenericDialog("BoxChecker");
    gd.addCheckbox("Set All", false);
    gd.addCheckbox("Clear All", false);
    gd.addCheckboxGroup(4, 5, labels, defaults);
    gd.addDialogListener(listener);
    gd.showDialog();

The control checkboxes should really be buttons (since their state is not persistent), but I didn't see how to add more buttons into a GenericDialog other than via GenericDialog.enableYesNoCancel(). I instead clear each of those after dealing with the CheckboxGroup.

Bill Christens-Barry

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