2011-08-17 12 views
0

groovy swing builderのfileChooserを使おうとしています。 私はGroovyのWebサイトから、次の例をコピーする場合:Groovy Swing buider fileChooser

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) { 

}

をしかし、私はエラーメッセージを得た:あなたはSwingBuilderの外にそのようにファイルチューザを使用することはできません

groovy.lang.MissingMethodException: No signature of method: static groovy.swing.SwingBuilder.fileChooser() is applicable for argument types: (java.util.LinkedHashMap, ConsoleScript19$_run_closure1) values 

答えて

5

を。その代わりに、通常のswingBuilder以外のJFileChooserを使用するだけです。 - 末尾の閉鎖はありませんnew 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() 

注:ここでは完全な作業例です。

3

NPEでOverZealousソリューションが失敗します。

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

NEW:fileFilter: [getDescription: {-> "*.xls"}, accept:{file -> file.toString() ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter

[Win7の(x64)のグルービー版:1.8.3 JVM:1.6.0_29)]

0

まず、SwingBuilderオブジェクトを作成する必要があると思います。これが私の仕事:

def swing = new SwingBuilder() 
def dialog = swing.fileChooser(dialogTitle: "Open A File") 
if (dialog.showOpenDialog() == JFileChooser.APPROVE_OPTION) { 
    println dialog.selectedFile 
} 

は、プロパティの完全なリストについては、hereを探してください。

関連する問題