Dialog box without 'OK' (single click on Radio button?) or shortcuts in dialog box?

Hi,
Does Fiji allow to create a dialog box with Radio button options but without having to press the ‘OK’ button every time an option is selected?
Also, is there any way I can add keyboard shortcuts to my radio button options?

Thanks!

Lee

2 Likes

I don’t think you can do this with macros. You’ll have to resort to one of the supported full-fledged scripting languages, such as Groovy or Jython.

Also, I don’t know of a way to hide the OK button (as long as you want to stick with GenericDialog that ensures your command will be recordable), but you can add a DialogListener that closes the dialog as soon as any option was selected.

This is a bit more complicated. You have to add a KeyListener to each radio button component and make sure to react to the respective key presses.

Here’s a Groovy script that illustrates how to do this with IJ1’s GenericDialog, an ij.gui.DialogListener and a java.awt.event.KeyListener:

import ij.gui.GenericDialog
import ij.gui.DialogListener
import java.awt.event.KeyListener

gd = new GenericDialog("Quick Radio Button Choice (QRBC)")

gd.addRadioButtonGroup("Your choice", ["One [1]", "Two [2]", "Many [3]"] as String[], 3, 1, null)
gd.hideCancelButton()
gd.setOKLabel("No need to press this button. Simply press 1, 2, or 3.")

// Close the dialog as soon as any item was changed.
gd.addDialogListener(new DialogListener() {
	boolean dialogItemChanged(ij.gui.GenericDialog dialog, java.awt.AWTEvent event) {
		dialog.dispose()
		return true
	}
})

// Make each radiobutton/checkbox listen to key-pressed events,
// select respective option, and close the dialog
checkboxGroup = gd.getRadioButtonGroups().get(0)
panel = gd.getComponents()[1]
checkboxes = panel.getComponents()

mylistener = new KeyListener() {
	void keyTyped(java.awt.event.KeyEvent event) {}
	void keyPressed(java.awt.event.KeyEvent event) {
		switch (event.getKeyChar()) {
			case '1':
			  checkboxGroup.setSelectedCheckbox(checkboxes[0])
			  gd.dispose()
			  break
			case '2':
			  checkboxGroup.setSelectedCheckbox(checkboxes[1])
			  gd.dispose()
			  break
			case '3':
			  checkboxGroup.setSelectedCheckbox(checkboxes[2])
			  gd.dispose()
			  break
			default:
			  break
		}
	}
	void keyReleased(java.awt.event.KeyEvent event) {}
}

checkboxes.each {
	it.addKeyListener(mylistener)
}

gd.showDialog()

// continue
println "You chose: " + gd.getNextRadioButton()

And this is how it looks:

Note that by messing around with specific Java AWT UI components, you’ll likely lose the capability of your script to run headlessly.