I'm trying to write a simple GUI to enter some data to an Imagej plugin using jython.
Some of the data to be entered are two directories (input and output) after clicking on two respective buttons. The problem I have is that I don't know how to assign a particular value obtained by getDirectory to a variable. If I set the variable "out" (within the class MyListener) as global I can use it but that way I can only use it only once as the second time it will erase the first directory.
The question would be how to make the class "MyListener" to return the chosen directory and set that to a particular variable.
The Generic Dialog code is the following:
from ij.gui import GenericDialog, DialogListener
from ij.io import DirectoryChooser
from java.awt import Button
from java.awt.event import ActionListener, ActionEvent
from ij.plugin.frame.Editor import actionPerformed
class MyListener (ActionListener):
def actionPerformed(self, event):
out = DirectoryChooser("Choose!!").getDirectory()
return out
def getOptions():
bt01 = Button("Get Directory")
bt02 = Button("Get Directory")
bt01.addActionListener(MyListener())
bt02.addActionListener(MyListener())
gd = GenericDialog("Directories")
gd.addStringField("FA Name", "File name")
gd.add(bt01)
gd.add(bt02)
gd.showDialog()
if gd.wasCanceled():
print "User canceled. Exiting...."
return
return bt01, bt02
options = getOptions()
if options is not None:
dir01, dir02 = options
print dir01, dir02'
Thank you!!
Use the getDirectory()
helper function in the ij.IJ
class:
from ij import IJ
from ij.gui import GenericDialog
def getOptions():
dir1 = IJ.getDirectory("Choose first directory")
dir2 = IJ.getDirectory("Choose second directory")
gd = GenericDialog("Directories")
gd.addStringField("FA Name", "File name")
gd.showDialog()
if gd.wasCanceled():
print "User canceled. Exiting...."
return
fileName = gd.getNextString()
return dir1, dir2, fileName
options = getOptions()
if options is not None:
dir01, dir02, name = options
print dir01, dir02, name
Alternatively, if you want to avoid the sequential popping up of dialogs and prefer a single dialog with all the options, you can use GenericDialogPlus
within Fiji. This class provides an addDirectoryField(String label, String defaultPath)
method.