Hello,
This is very urgent, so any help would be greatly appreciated! I am coding an ImageJ plug-in and when I open a dialog box to ask the user to select something, I can't seem to terminate it when the user clicks the red x in the upper right hand corner, nor when the user selects the Cancel button. 1) IJ.showMessageWithCancel does return false when Cancel is selected, but I think the plugin is called again and again and so the program doesn't actually end. Is there a solution for this? 2) Is there a way for the code to return when the user selects the red x? Thank you so much! |
On Sun, July 12, 2015 04:58, hlee19 wrote:
> Hello, > This is very urgent, so any help would be greatly appreciated! > > I am coding an ImageJ plug-in and when I open a dialog box to ask the user > to select something, I can't seem to terminate it when the user clicks the > red x in the upper right hand corner, nor when the user selects the Cancel > button. > > 1) IJ.showMessageWithCancel does return false when Cancel is selected, but > I > think the plugin is called again and again and so the program doesn't > actually end. Is there a solution for this? > 2) Is there a way for the code to return when the user selects the red x? Hi anonymous, The IJ.showMessageWithCancel method is a GenericDialog, like most ImageJ dialogs. For sure, it does not reappear. If it reappears, this must be your code that calls it again. Quickly looking at the code, it seems to me that closing the dialog box of IJ.showMessageWithCancel makes it return true. This makes sense if the user should 'cancel' only in special cases. If you want it to return false when closed without pressing a button, simply copy the code from IJ.showMessageWithCancel and return gd.wasOKed(), e.g.: /** Displays a message in a dialog box with the specified title. Returns true only if the user pressed "OK". */ static boolean showMessageReturnOKed(String title, String msg) { GenericDialog gd = new GenericDialog(title); gd.addMessage(msg); gd.showDialog(); return gd.wasOKed(); } Michael -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hello,
Thank you for the advice. The thing is, I would like to let the user cancel/exit the program in the middle, instead of being forced to keep selecting files/enter input. Could you please let me know if there's anything wrong with my running the program as follows?: -Copy the java files from Eclipse into a folder -Place the folder in ImageJ's Plugins folder -Open ImageJ -Open the needed .im image -Compile and run the one .java file (the rest are files with classes that determine a new Object or txt files) My program takes an input txt file, finds the values the user wants to merge into one (i.e., the values 5 and 47 become 6000), creates and saves a new txt file to reflect that, uses the opened image's pixel values to do the same with the values (i.e., the pixels with values 5 and 47 have 6000 instead), and saves and opens the new image. I would also then like it to ask the user if he/she would like to run the plugin again, but that would be the next bit after fixing this bug. Here is what the main part of the code looks like (before implementing your suggestion): public int setup(String arg, ImagePlus imp) { return DOES_STACKS+DOES_32; } public void run(ImageProcessor ip){ if(!IJ.showMessageWithCancel("Merge Sections", "Please input the values you wish to combine into one section, each separated ONLY by a space.")) return; //I would like the user to be able to exit the program at these return statements; but when the user clicks Cancel/exits, the program doesn't exit completely but rather comes back to run this method again String inputVals = IJ.getString("Values ", "0"); if(inputVals.equals("")) return; String[] numToMerge = inputVals.split("\\s+"); //put the values into a String array if(!IJ.showMessageWithCancel("Merge Sections", "Values entered: " + inputVals + "\nPlease select a .txt file that contains the section names and values.")) return; OpenDialog od = new OpenDialog("Please select a .txt file that contains the section names and values."); File file = new File(od.getPath()); //file contains everything in the user-selected file String fileName = od.getFileName(); if(fileName == null || !IJ.showMessageWithCancel("Merge Sections", "File selected: " + file.getName())) return; DoublyLinkedList list = new DoublyLinkedList(); DoublyLinkedList updatedList = new DoublyLinkedList(); int mergedSecNum = 6000; //to be the starting number of the merged sections //updatedList has the merged sections; is the new list try { updatedList = updateList(file, list, mergedSecNum, numToMerge, updatedList); } catch (FileNotFoundException e) { GenericDialog d = new GenericDialog(""); d.addMessage("File not found!"); d.showDialog(); if(d.wasCanceled()) return; } ArrayList<String> al = new ArrayList<String>(); ListNode cur = updatedList.getFirst(); while(cur != null){ String s = cur.getValue().getName() + "\t" + cur.getValue().getNumber(); al.add(s); cur = cur.getNext(); } TextPanel p = new TextPanel(""); p.append(al); String[] sepFileName = removeFileExt(fileName); p.saveAs("C:\\Program Files (x86)\\ImageJ\\plugins\\Merge\\"+sepFileName[0]+((mergedSecNum-5990)/10)+"."+sepFileName[1]); ImagePlus imp = mergeInImage(numToMerge, mergedSecNum); imp.show(); imp.unlock(); return; } Thank you very much, and please let me know if anything is obscure. |
Free forum by Nabble | Edit this page |