如何制作“打开方式"对话框? [英] How to make the 'open with' dialog box?

查看:25
本文介绍了如何制作“打开方式"对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I made a program to search for .txt files.

If I click a file it means that the "open with" dialog box should appear, and that dialog box will contain a list of all installed programs.

I am using this code for searching through the files:

  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");
  } 

What Java code can I use to make the "open with" dialog box appear?

解决方案

You should use FileChooser for this. Take a look 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);
        }
   } ...
}

这篇关于如何制作“打开方式"对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆