SWT保存对话框

public class SafeSaveDialogUp {
    public SafeSaveDialogUp(){
    }
    FileDialog dlg;

    public SafeSaveDialogUp(Shell shell,String fileName) {
        dlg = new FileDialog(shell, SWT.SAVE);// 设置为保存对话框
        // 设置保存类型
        dlg.setFileName(fileName);
       
        dlg.setFilterNames(new String[] {
                  "Excel Files (*.xsl)",           //这样输入文件名时不用加.xls
                  "All Files (*.*)"
                });
                dlg.setFilterExtensions(new String[] {
                   "*.xsl",  "*.*"     
                });
    }

    public String open() {
        // Store the selected file name in fileName
        String saveFileName = null;

        // The user has finished when one of the
        // following happens:
        // 1) The user dismisses the dialog by pressing Cancel
        // 2) The selected file name does not exist
        // 3) The user agrees to overwrite existing file
        boolean done = false;

        while (!done) {
            // Open the File Dialog
            saveFileName = dlg.open();  //得到输入的文件名,输入文件名时不用加.xls
            //取消保存对话框
            if (saveFileName == null) {
                // User has cancelled, so quit and return
            	MessageBox mg = new MessageBox(dlg.getParent(),
                        SWT.ICON_WARNING| SWT.YES);
            	mg.setText("Tips");
                mg.setMessage("你取消了保存文件");
                done = mg.open() == SWT.YES;
                done = true;
                
                
            } 
            else 
            {
                // 文件名已经存在,是否替换
                File file = new File(saveFileName);
                if (file.exists()) {
                    // The file already exists; asks for confirmation
                    MessageBox mb = new MessageBox(dlg.getParent(),
                            SWT.ICON_WARNING | SWT.YES | SWT.NO);
                    mb.setText("Tips");
                    mb.setMessage(saveFileName
                            + " 已经存在,是否要替换它?");

                    // If they click Yes, drop out. If they click No,
                    // redisplay the File Dialog
                    done = mb.open() == SWT.YES;
                } else {
                    // 不存在文件名重复,可以保存
                    done = true;
                }
            }
          
        }
        return saveFileName;
    }
    public String getFileName() {
        return dlg.getFileName();
      }

      public String[] getFileNames() {
        return dlg.getFileNames();
      }

      public String[] getFilterExtensions() {
        return dlg.getFilterExtensions();
      }

      public String[] getFilterNames() {
        return dlg.getFilterNames();
      }

      public String getFilterPath() {
        return dlg.getFilterPath();
      }

      public void setFileName(String string) {
        dlg.setFileName(string);
      }

      public void setFilterExtensions(String[] extensions) {
        dlg.setFilterExtensions(extensions);
      }

      public void setFilterNames(String[] names) {
        dlg.setFilterNames(names);
      }

      public void setFilterPath(String string) {
        dlg.setFilterPath(string);
      }

      public Shell getParent() {
        return dlg.getParent();
      }

      public int getStyle() {
        return dlg.getStyle();
      }

      public String getText() {
        return dlg.getText();
      }

      public void setText(String string) {
        dlg.setText(string);
      }
      /**
         * Runs the application
         */
      
      /**
         * Creates the window contents
         * 
         * @param shell the parent shell
         */
      public  void openSaveFileDialog(String fileName) {
      
           Shell shell=new Shell();  
    	  SafeSaveDialogUp dlg = new SafeSaveDialogUp(shell,fileName);
              
             
              dlg.open();
            
         
      }

     
   
}


调用openSaveFileDialog函数即可

参考:http://www.java2s.com/CN/Code/Java/SWT-JFace-Eclipse/AfacadeforthesaveFileDialog.htm 

http://blog.csdn.net/techyang/article/details/1445521

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值