【问题标题】:How to make the 'open with' dialog box?如何制作“打开方式”对话框?
【发布时间】:2013-05-14 11:47:25
【问题描述】:

我编写了一个搜索 .txt 文件的程序。

如果我单击一个文件,这意味着应该出现“打开方式”对话框,并且该对话框将包含所有已安装程序的列表。

我正在使用此代码搜索文件:

  public File[] finder( String dirName)
  {
      // Create a file object on the directory.
      File dir = new File(dirName);
      // Return a list of all files in the directory.
      return dir.listFiles(new FilenameFilter();
  } 

  public boolean accept(File dir, String filename)
  { 
      return filename.endsWith(".txt");
  } 

我可以使用什么 Java 代码来显示“打开方式”对话框?

【问题讨论】:

  • 你有没有尝试过?这是谷歌的第一个结果:docs.oracle.com/javase/tutorial/uiswing/components/…
  • JFileChooser Filters的可能重复
  • 检查我的编辑。经过仔细阅读,我认为这就是 OP 想要的。
  • 我不想要文件选择器..我需要那个对话框只包含操作系统中已安装的程序..
  • @user2364985 你必须自己实现它。请参阅我的回答,我提供了在 Windows 上执行此操作的链接

标签: java swing dialog


【解决方案1】:

您应该为此使用FileChooser。看看here

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);


public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
   } ...
}

【讨论】:

【解决方案2】:

我可以使用什么 Java 代码来显示“打开方式”对话框?

据我所知,J2SE 中没有这样的东西。 OTOH Desktop API 可以在任何应用程序中打开 File。是默认消费者。

【讨论】:

    【解决方案3】:

    您可以为此目的制作自己的对话框。接下来如何获取程序列表。在 Windows 上,您可以使用注册表。看这个链接Detecting installed programs via registry

    并检查如何通过 java 访问注册表 read/write to Windows Registry using Java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 2016-04-10
      • 2018-03-18
      • 1970-01-01
      • 2020-12-23
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多