Odd behavior from Threshold
Posted by Bob Loushin on Nov 18, 2011; 11:56pm
URL: http://imagej.273.s1.nabble.com/Odd-behavior-from-Threshold-tp3682526.html
I am writing a plugin that needs to fit into the following workflow:
User opens image.
User runs plugin.
The plugin opens the threshold window, and waits until the user has made an adjustment and hit "Apply".
The plugin does the thresholding, then does other stuff, then ends.
User saves image, opens new one, runs plugin again....
Here's the part of the code that does the thresholding:
boolean done = false;
ThresholdAdjuster thresh = new ThresholdAdjuster();
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
while (!done)
{
IJ.wait(1000); //Take a break between checks so as not to bog the system.
done = true;
Thread[] threads = new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads);
for (i=0; i<threads.length; i++)
if (threads[i].getName().equals("ThresholdAdjuster")) done = false;
}
The first time through, all is well. But the second time, it fails, depending on how I handle it.
1) If I close the threshold window by clicking on the x in the upper right corner of the window before I run the plugin the second time, all is well. But having to manually close this window after every run is annoying, and I'd like to eliminate this step.
2) If I leave the threshold window open while I open the second image and rerun the plugin, the threshold window is brought to the front, but the plugin does not wait for user input--it just moves on and tries to process the unthresholded image.
3) If I add a line to the plugin, after the while loop, like thresh.dispose() or thresh.setVisible(false), then the next time the plugin is run, the threshold window does not come back, and the plugin again tries to process the unthresholded image.
4) If I leave in the thresh.setVisible(false) line, and add thresh.setVisible(true) just before the while loop, the threshold window still does not come back, and the plugin again tries to process the unthresholded image.
Is there a way to do this that doesn't require the user to constantly close the Threshold window? It doesn't really matter if it is left open between runs (and waited for every time!) or is closed by the plugin, then reopened successfully (and waited for!) with each run.
Thank you!