Groovy Swing buider fileChooser

Ara*_*ash 1 groovy swingbuilder

我试图使用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) {
Run Code Online (Sandbox Code Playgroud)

}

但是我收到了一条错误消息:

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

Ove*_*ous 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()
Run Code Online (Sandbox Code Playgroud)

请注意,new JFileChooser正好以右括号结束 - 没有尾随闭包.