john renfrew Posted May 18, 2010 Posted May 18, 2010 Is there an easy way to add a FileFilter to the Choose Folder example which I have now turned into a Choose File example, preferably by taking a single or list of file endings (for example to just select all PDF files)?? THe examples I have tried to apply all seem to be trying to extend the class and as a result nothing I do seems to bring me anything other than exceptions
Smef Posted May 18, 2010 Posted May 18, 2010 The Choose Folder module does not currently support requiring a single file type. We may be able to customize this module for you, though. If you are interested, send an email to [email protected] with what you are looking for and we will get you an estimate for the work.
john renfrew Posted May 20, 2010 Author Posted May 20, 2010 Nothing like that to open the books and do it one's self... So here is something which does the job and more, not necessarily that efficient Would be really helpful for someone to write a book on swingbuilder as the methods and syntax are quite different but it does allow for proper extension of the FM interface in ways that are , as shown here, highly programmable. // ChooseFiltered(fm_Path, fm_Message, fm_FileType, fm_Button, fm_Select) // fm_Path startpath for dialog // fm_Message for dialog title // fm_Button sets text for default 'accept' button // fm_FileType selects file filter - p == pdf, i == images // ... d == documents, x == data files, z == archives, a == all filters, null == no filters // fm_Select is flag to set what is selected - f == files, d == dirs, null == both import javax.swing.JFileChooser import javax.swing.filechooser.FileNameExtensionFilter chooser = new JFileChooser() if (fm_Select == "d"){ chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ) } if (fm_Select == "f"){ chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ) } else chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ) if( fm_Path != null ) chooser.setCurrentDirectory( new File(fm_Path) ) if( fm_Message != null ) chooser.setDialogTitle( fm_Message ) //chooser.setFileHidingEnabled(false) // sets whether or not hidden files are shown pdf = new FileNameExtensionFilter( "PDF files", "pdf" ) img = new FileNameExtensionFilter( "Image files", "jpg", "gif", "jpeg", "png", "tif", "bmp" ) doc = new FileNameExtensionFilter( "Documents", "doc", "docx", "txt", "wri", "htm", "html", "css", "log" ) xml = new FileNameExtensionFilter( "Data files", "csv", "xls", "xlsx", "xml", "tab", "xsl" ) zip = new FileNameExtensionFilter( "Archives", "zip", "dmg", "jar" ) if ( fm_FileType == "i"){ chooser.setFileFilter(img) } if ( fm_FileType == "d"){ chooser.setFileFilter(doc) } if ( fm_FileType == "p"){ chooser.setFileFilter(pdf) } if ( fm_FileType == "x"){ chooser.setFileFilter(xml) } if ( fm_FileType == "z"){ chooser.setFileFilter(zip) } if ( fm_FileType == "a"){ chooser.addChoosableFileFilter(img) chooser.addChoosableFileFilter(doc) chooser.addChoosableFileFilter(pdf) chooser.addChoosableFileFilter(xml) chooser.addChoosableFileFilter(zip) chooser.setAcceptAllFileFilterUsed(true) } else if ( fm_FileType == null){ chooser.setAcceptAllFileFilterUsed(true) } chooser.setApproveButtonText(fm_Button) chooser.setApproveButtonToolTipText("Import this file") //chooser.setAcceptAllFileFilterUsed(false) if( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) { // select depending on format you require to be returned //return chooser.getSelectedFile().getCanonicalPath() return chooser.getSelectedFile().getAbsolutePath() } else return false
john renfrew Posted May 20, 2010 Author Posted May 20, 2010 Simpler version which just accepts a list of file types and sets the selection list item text // ChooseNarrow(fm_Path, fm_Message, fm_Button, fm_FileType, fm_Select) // fm_Path startpath for dialog // fm_Message for dialog title // fm_Button sets text for default 'accept' button // fm_FileType selects description in chooser, null shows all // fm_Select is list for file types to be choosable import javax.swing.JFileChooser import javax.swing.filechooser.FileNameExtensionFilter list = fm_Select.split("n") chooser = new JFileChooser() chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ) if( fm_Path != null ) chooser.setCurrentDirectory( new File(fm_Path) ) if( fm_Message != null ) chooser.setDialogTitle( fm_Message ) //chooser.setFileHidingEnabled(false) // sets whether or not hidden files are shown if ( fm_FileType != null){ filt = new FileNameExtensionFilter( fm_FileType, list) chooser.setFileFilter(filt) } else { chooser.setAcceptAllFileFilterUsed(true) } chooser.setApproveButtonText(fm_Button) chooser.setApproveButtonToolTipText("Import this file") //chooser.setAcceptAllFileFilterUsed(false) if( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) { // select depending on format you require to be returned //return chooser.getSelectedFile().getCanonicalPath() return chooser.getSelectedFile().getAbsolutePath() } else return false
Noél Dubau Posted July 31, 2010 Posted July 31, 2010 Hello, I found that post while I'm trying to pick a file and his path. It seems possible, but I'm too novice to understand what parameters must be given before running the scripts of J. Renfrew Can someone help me ? Thanks Noël
john renfrew Posted August 1, 2010 Author Posted August 1, 2010 Try this Create a new function with the following prameters on the left fm_Path fm_Message fm_Button fm_FileType fm_Select and in each box put a: a valid path b: Select a file c: Pick me! d: pdf e: f and then try it that should select all pdf files in the path you first gave it change each variable one at a time and watch what happens... e: will take f for files or d for directories or null for both
Noél Dubau Posted August 1, 2010 Posted August 1, 2010 Hello John ! First I thank you for that answer ; since I posted my question, I had understand these parameters. As you seem say that a null parameter is possible for file/folder, would it be the same for the extension : extension empty = all file types like in dos for jokers ? Thanks ! Noël Excuse my english !
john renfrew Posted August 1, 2010 Author Posted August 1, 2010 (edited) The lines in the code if ( fm_FileType != null){ filt = new FileNameExtensionFilter( fm_FileType, list) chooser.setFileFilter(filt) } else { chooser.setAcceptAllFileFilterUsed(true) } says... If you provide a file type - say pdf it will use that in a FileFilter otherwise if null it will just show all files This sets the LABEL in the chooser dialog to show what files will be chosen The variable fm_Select will accept a list of items So you could say it was doc files and give it a list of doc wri txt docx and it would show up all of those files in the chooser Edited August 1, 2010 by Guest
Noél Dubau Posted August 4, 2010 Posted August 4, 2010 Hello, Wasn't home for two days... I tried as you saif but an error always occurs. I tried many ways for the path including long or short /Volumes/Master/GestBCD2009_encours/... but always the error at the beginning... org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 10: unable to resolve class javax.swing.filechooser.FileNameExtensionFilter @ line 10, column 1. import javax.swing.filechooser.FileNameExtensionFilter ^ 1 error Parameters: {fm_Path=/GestBCD2009_encours, fm_Message=Choisir un fichier, fm_Button=Sélectionner, fm_FileType=pdf, fm_Select=f} ---Script--- Script: // ChooseFiltered(fm_Path, fm_Message, fm_FileType, fm_Button, fm_Select) // fm_Path startpath for dialog // fm_Message for dialog title // fm_Button sets text for default 'accept' button // fm_FileType selects file filter - p == pdf, i == images // ... d == documents, x == data files, z == archives, a == all filters, null == no filters // fm_Select is flag to set what is selected - f == files, d == dirs, null == both import javax.swing.JFileChooser import javax.swing.filechooser.FileNameExtensionFilter chooser = new JFileChooser() if (fm_Select == "d"){ chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ) } if (fm_Select == "f"){ chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ) } else chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ) if( fm_Path != null ) chooser.setCurrentDirectory( new File(fm_Path) ) if( fm_Message != null ) chooser.setDialogTitle( fm_Message ) //chooser.setFileHidingEnabled(false) // sets whether or not hidden files are shown pdf = new FileName... Open my eyes please ! Noël
fseipel Posted August 4, 2010 Posted August 4, 2010 Noel, The code seems to work fine, I tried it on Windoze with c: path, with fm_FileType p for pdf -- very nice! Note that the code, as originally posted, was missing the semicolons at the end of each executable line. This is the reason you received an error on the first executable line. A copy of the identical program with semicolons appears below. Somehow semicolons must have been stripped by the author's IDE? // ChooseFiltered(fm_Path, fm_Message, fm_FileType, fm_Button, fm_Select) // fm_Path startpath for dialog // fm_Message for dialog title // fm_Button sets text for default 'accept' button // fm_FileType selects file filter - p == pdf, i == images // ... d == documents, x == data files, z == archives, a == all filters, null == no filters // fm_Select is flag to set what is selected - f == files, d == dirs, null == both import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; chooser = new JFileChooser(); if (fm_Select == "d"){ chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ); } if (fm_Select == "f"){ chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); } else chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ); if( fm_Path != null ) chooser.setCurrentDirectory( new File(fm_Path) ); if( fm_Message != null ) chooser.setDialogTitle( fm_Message ); //chooser.setFileHidingEnabled(false) // sets whether or not hidden files are shown pdf = new FileNameExtensionFilter( "PDF files", "pdf" ); img = new FileNameExtensionFilter( "Image files", "jpg", "gif", "jpeg", "png", "tif", "bmp" ); doc = new FileNameExtensionFilter( "Documents", "doc", "docx", "txt", "wri", "htm", "html", "css", "log" ); xml = new FileNameExtensionFilter( "Data files", "csv", "xls", "xlsx", "xml", "tab", "xsl" ); zip = new FileNameExtensionFilter( "Archives", "zip", "dmg", "jar" ); if ( fm_FileType == "i"){ chooser.setFileFilter(img); } if ( fm_FileType == "d"){ chooser.setFileFilter(doc); } if ( fm_FileType == "p"){ chooser.setFileFilter(pdf); } if ( fm_FileType == "x"){ chooser.setFileFilter(xml); } if ( fm_FileType == "z"){ chooser.setFileFilter(zip); } if ( fm_FileType == "a"){ chooser.addChoosableFileFilter(img); chooser.addChoosableFileFilter(doc); chooser.addChoosableFileFilter(pdf); chooser.addChoosableFileFilter(xml); chooser.addChoosableFileFilter(zip); chooser.setAcceptAllFileFilterUsed(true); } else if ( fm_FileType == null){ chooser.setAcceptAllFileFilterUsed(true); } chooser.setApproveButtonText(fm_Button); chooser.setApproveButtonToolTipText("Import this file"); //chooser.setAcceptAllFileFilterUsed(false) if( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) { // select depending on format you require to be returned //return chooser.getSelectedFile().getCanonicalPath() return chooser.getSelectedFile().getAbsolutePath(); } else return false;
Noél Dubau Posted August 4, 2010 Posted August 4, 2010 (edited) Hello Frank ! I'm glad to have an answer from you (we've had contacts sone months ago about a script for Amazon and your help was a pleasure)... I just copied your code and get the same error ; I I ask for more infos I get the scree copy joined... I'm going to try under WinXP... Noël [color:blue]Coming back after opening the file under Windows : that works fine too. So I ask about the correct way to indicate the path under MacOS : /Volumes/Master/GestBCD2009/ /Master/GestBCD2009/ /GestBCD2009/ another syntax ? Edited August 4, 2010 by Guest
fseipel Posted August 4, 2010 Posted August 4, 2010 Noel, Hi again, I remember you from our earlier correspondence. I've been doing more work with Amazon and Filemaker, I recently set up FM to be able to make Amazon Marketplace Web Service calls. I'm not certain (I don't have a Mac here) but I believe problem may be you don't have the latest version of Java, version 6, on your Mac. http://www.javatester.org/version.html to check. e.g. 1.6 = version 6. FileNameExtensionFilter is available only in Java 6. The semicolons are still needed. So the error would be consistent with this. The error you're getting isn't during runtime, so the path spec isn't causing it.
Noél Dubau Posted August 5, 2010 Posted August 5, 2010 Hello Frank, I have an iMac under MacOS 10.5.8 ; if I ask for java version the terminal returns me java version "1.5.0_24" ; but when I ask the Update softs from Apple Menu, nothing concerning Java is avalaible... Snifff ! Noël
Noél Dubau Posted August 5, 2010 Posted August 5, 2010 Mystery ! Since yesterday, and following advices of F. Seipel I did the following tests * I put the version 1.6 at the top of the lists in Java preferences... restarted the iMac and the ScriptMaster : no success... * After having placed Java 1.6 at the top and restarted my iMac Intel I asked the terminal and it returned me the 1.6 for Java version (image 4). * When going on the test page (http://www.javatester.org/version.html)I got Image 5 * I turned in the configuration of Java to uncheck versions below 1.6, restarted: the terminal gives me always the version 1.6... and the test page nothing (Image 6) ?? Archive.zip
john renfrew Posted November 22, 2010 Author Posted November 22, 2010 This is a 10.5.8 issue.. Not sure why but the JVM that FM fires up will only be 1.5.xxxx not 1.6.xxx even if it is at the top of the list for function which need this later version of Java I am adding this at the top if (System.getProperty("java.version").substring(0, 2) == "1.5"){ return 'ERROR' and you DO NOT need the semi-colons at the end of each line, only when separating things within one line. That is straight out of the Groovy documentation
john renfrew Posted November 22, 2010 Author Posted November 22, 2010 "Groovy uses a similar syntax to Java although in Groovy semicolons are optional. This saves a little typing but also makes code look much cleaner (surprisingly so for such a minor change). So normally if one statement is on each line you can ommit semicolons altogether - though its no problem to use them if you want to. If you want to put multiple statements on a line use a semicolon to separate the statements." http://groovy.codehaus.org/Statements
Recommended Posts
This topic is 5127 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now