SWT打开对话框

来源:互联网 发布:js 添加style属性 编辑:程序博客网 时间:2024/06/16 00:00

以打开pdf文件为例,安装windowbuilder,新建swt/jface项目,在新建swt Application Window,

 

public class OpenPdfUpdate { /**    *@see 初始化打开文件对话框,改写csharp    *@param 是否允许多选    *@return 返回打开文件的文件名    */ public  String[] InitOpenFileDialog(boolean isMultiselect) { Display display =null; Shell shell =null; FileDialog dialog=null; try { display = new Display();shell = new Shell(display);String[] fileNames =null;if (isMultiselect == false) {dialog = new FileDialog(shell, SWT.OPEN);//Set dialog titledialog.setText("请选择要打开的单个文件");dialog.setFilterPath("c:\windows");//设置初始打开文件的路径// Set filter on .pdf filesdialog.setFilterExtensions(new String[] { "*.pdf", "*.*" });//Put in a readable name for the filterdialog.setFilterNames(new String[] { "Pdf Files (*.pdf)","All Files (*.*)" });fileNames=new String[1];fileNames[0]= dialog.open(); // 返回的全路径(路径+文件名)if (fileNames[0] == null) {             // User has cancelled, so quit and return         MessageBox mg = new MessageBox(dialog.getParent(),                     SWT.ICON_WARNING| SWT.YES);         mg.setText("Tips");             mg.setMessage("没有指定要打开的文件,或取消了打开文件!");             boolean done=mg.open() == SWT.YES; }}if (isMultiselect == true) {dialog = new FileDialog(shell, SWT.MULTI);dialog.setText("请选择要打开的文件,按住Shift连续多选,按住Ctrl间隔多选");dialog.setFilterExtensions(new String[] { "*.pdf", "*.*" });dialog.setFilterNames(new String[] { "Pdf Files (*.pdf)","All Files (*.*)" });//return last file nameString saveFileName=dialog.open();//返回最后一个选择文件的全路径 if (saveFileName == null) {             // User has cancelled, so quit and return         MessageBox mg = new MessageBox(dialog.getParent(),                     SWT.ICON_WARNING| SWT.YES);         mg.setText("Tips");             mg.setMessage("没有指定要打开的文件,或取消了打开文件!");             boolean done=mg.open() == SWT.YES; } // root String path = dialog.getFilterPath();//返回选择的路径,       这个和fileNames配合可以得到所有的文件的全路径//all file namefileNames = dialog.getFileNames();for (int index = 0; index < fileNames.length; index++) {fileNames[index] = path + fileNames[index];}}return fileNames;}  finally{       display.dispose();    } }/**    *@see 通过打开文件对话框,用户单选文件,返回用户选择的文件名,包括完整路径    *@return 完整路径的通过对话框选中的文件名字符串    */public String[] readFileName(){String[] singleFileName=InitOpenFileDialog(false);    return singleFileName;    }/**    *@see 这里要求输入的对话框是多选,返回是选中的文件完整名构成的数据组    *@return 完整路径的通过对话框选中的文件名字符串数组    */public String[] readFileNames(){String[] multiFileName=InitOpenFileDialog(true);       return multiFileName;}public static void main(String[] args){OpenPdfUpdate op=new OpenPdfUpdate();//读多个文件String[] mulFileName=op.readFileNames();for(int i=0;i<mulFileName.length;i++){System.out.println(mulFileName[i]);}//读一个文件String[] oneString=op.readFileName();System.out.println(oneString[0]);}}参考:http://www.java2s.com/CN/Tutorial/Java/0280__SWT/OpenanOpenFileDialog.htm参考:http://www.blogjava.net/dreamstone/archive/2007/08/09/134536.html


 

原创粉丝点击