天天看點

SWT打開對話框

以打開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 title
					dialog.setText("請選擇要打開的單個檔案");
					dialog.setFilterPath("c:\windows");//設定初始打開檔案的路徑
					// Set filter on .pdf files
					dialog.setFilterExtensions(new String[] { "*.pdf", "*.*" });
					//Put in a readable name for the filter
					dialog.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 name
					String 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 name
					fileNames = 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