2015-04-01 26 views
1
browseButton = new Button(controlGroupForSingleRun, SWT.PUSH); 
    browseButton.setText("Browse ..."); 
    data = new GridData(); 
    data.widthHint = 80; 
    browseButton.setLayoutData(data); 
    browseButton.addSelectionListener(new SelectionListener(){ 
     public void widgetSelected(SelectionEvent event){ 
      lookForExeClick(); 
     } 
     public void widgetDefaultSelected(SelectionEvent event){ 
      widgetSelected(event); 
     } 
    }); 

此代码在Eclipse插件GUI上生成一个浏览按钮,如何使此按钮只选择一个目录而不是特定的文件。现在它正在选择一个文件。有人可能会向我提供修改过的代码以允许这种情况发生。谢谢。此代码创建一个浏览按钮以选择一个文件。我如何更改它以允许我选择文件夹而不是文件夹内的文件

这里要求的是lookforExClick()方法。

So I changed the filedialog to directorydialog as the answer below mentioned. But here is my method you asked for: 

    private void lookForExeClick(){ 
    boolean notLegal = false; 
    while(!notLegal){ 
     exeChooser.setText("Choose an executable file"); 

     singleExeFilePath = exeChooser.open(); 

     String tmp = exeChooser.getFileName(); 

     if(tmp.equals("")){ 
      notLegal = true; 
      return; 
     } 
     else if(tmp == null){ 
      notLegal = true; 
      return; 
     } 
     if(!tmp.equals("") && !tmp.endsWith(".exe") && !tmp.endsWith(".bat") && !tmp.endsWith(".jar")){ 
      MessageDialog.openInformation(
       window.getShell(), 
       "Vulnerabilities Viewer", 
       "Please input a legal executable name!" 
       ); 
     } 
     else { 
      notLegal = true; 
     } 
    } 

    exeLocationText.setText(singleExeFilePath); 
    pureExeFileName = exeChooser.getFileName(); 

    // Set selection to newLocation. 
    last.setSelection(false); 
    for(int i = 0; i < radios.length; i++){ 
     radios[i].setSelection(false); 
    } 
    newLocation.setSelection(true); 
} 

我该如何取出或发表评论才能使浏览按钮适用于目录选择。请帮帮我。谢谢。

+0

您显示的代码不会选择任何内容,请向我们展示“lookForExeClick”方法,其中我假设实际做了选择。 – 2015-04-01 07:16:24

+0

按要求我添加了方便您的方法。 – 2015-04-02 02:09:16

回答

0

这是一个带DirectoryDialog的片段,用它代替FileDialog。

DirectoryDialog dlg = new DirectoryDialog(Display.getCurrent().getActiveShell()); 
    String directoryPath = dlg.open(); 
    System.out.println(directoryPath); 

编辑:现在你添加的lookForExeClick()这里是我会做什么,如果我理解你的代码的权利:

private void lookForExeClick() { 
    DirectoryDialog exeChooser = new DirectoryDialog(Display.getDefault().getActiveShell()); 

    String pureExeFileName = null; 

    String directoryPath = exeChooser.open(); 
    File directory = new File(directoryPath); 
    for (File file : directory.listFiles()) { 
     String fileName = file.getName(); 
     if (fileName.endsWith(".exe") || fileName.endsWith(".bat") || fileName.endsWith(".jar")) { 
      pureExeFileName = fileName; 
      break; 
     } 
    } 

    if (pureExeFileName == null) { 
     MessageDialog.openInformation(window.getShell(), "Vulnerabilities Viewer", "NO exe file found in directory"); 
     return; 
    } 

    exeLocationText.setText(pureExeFileName); 

    // Set selection to newLocation. 
    last.setSelection(false); 
    for (int i = 0; i < radios.length; i++) { 
     radios[i].setSelection(false); 
    } 
    newLocation.setSelection(true); 
} 

编辑:基于您的评论,我会做这样的事情:

DirectoryDialog dlg = new DirectoryDialog(Display.getCurrent().getActiveShell()); 
    String directoryPath = dlg.open(); 
    File file = new File(directoryPath, "MyFileName.exe"); 

    // if you already have the generated file on the file system 
    Files.copy(sourceFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); 

    // if you will generate the straight to a stream 
    FileOutputStream outputStream = new FileOutputStream(file); 
+0

确定那么我该怎么改变这一行String tmp = exeChooser.getFileName();有没有getdirectory或什么的?谢谢。 – 2015-04-02 01:51:58

+0

我看你做了什么。谢谢。但是,它仍然在寻找一个exe文件在文件夹中。我想要的是brose按钮将允许我选择一个目录并保存在该目录中生成的文件。不要选择一个.exe文件。 根据您提供的代码,我可以选择一个目录,但它会查找文件夹中的.exe文件。我如何解决这个问题?任何帮助,将不胜感激。谢谢。 – 2015-04-05 17:43:14

+0

您会在哪里添加此代码?在lookForExeClick()方法中?我将生成一个不输入文件的文件。请指教。谢谢你的帮助。 – 2015-04-08 01:22:49

相关问题