我试图使用groovy swing builder的fileChooser而没有运气。 当我从groovy网站复制以下示例时:
def openExcelDialog = SwingBuilder.fileChooser(dialogTitle:"Choose an excel file",
id:"openExcelDialog", fileSelectionMode : JFileChooser.FILES_ONLY,
//the file filter must show also directories, in order to be able to look into them
fileFilter: [getDescription: {-> "*.xls"}, accept:{file-> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter) {
}
但是我收到了一条错误消息:
groovy.lang.MissingMethodException: No signature of method: static groovy.swing.SwingBuilder.fileChooser() is applicable for argument types: (java.util.LinkedHashMap, ConsoleScript19$_run_closure1) values
答案 0 :(得分:5)
你不能在SwingBuilder之外使用fileChooser。相反,你需要使用普通的非swingBuilder JFileChooser。这是一个完整的工作示例:
import javax.swing.filechooser.FileFilter
import javax.swing.JFileChooser
def openExcelDialog = new JFileChooser(
dialogTitle: "Choose an excel file",
fileSelectionMode: JFileChooser.FILES_ONLY,
//the file filter must show also directories, in order to be able to look into them
fileFilter: [getDescription: {-> "*.xls"}, accept:{file-> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter)
openExcelDialog.showOpenDialog()
请注意,new JFileChooser
只是以右括号结束 - 没有尾随闭包。
答案 1 :(得分:3)
OverZealous'解决方案因NPE而失败。
Exception in thread "Basic L&F File Loading Thread" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Unknown Source)
at java.util.regex.Matcher.reset(Unknown Source)
at java.util.regex.Matcher.<init>(Unknown Source)
at java.util.regex.Pattern.matcher(Unknown Source)
at org.codehaus.groovy.runtime.InvokerHelper.matchRegex(InvokerHelper.java:335)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.matchRegex(ScriptBytecodeAdapter.java:722)
at ExcelChooser2$_run_closure2.doCall(ExcelChooser2.groovy:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at FileFilter_groovyProxy.accept(Script1.groovy:7)
at javax.swing.JFileChooser.accept(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run0(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(Unknown Source)
<强>解决方案:强>
OLD:fileFilter: [getDescription: {-> "*.xls"}, accept:{file -> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter
新:fileFilter: [getDescription: {-> "*.xls"}, accept:{file -> file.toString() ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter
[Win7(x64)Groovy版本:1.8.3 JVM:1.6.0_29)]
答案 2 :(得分:1)
我认为你需要先创建一个SwingBuilder对象。这对我有用:
def swing = new SwingBuilder()
def dialog = swing.fileChooser(dialogTitle: "Open A File")
if (dialog.showOpenDialog() == JFileChooser.APPROVE_OPTION) {
println dialog.selectedFile
}
查看here以获取完整的属性列表。