-1

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?

ollo
  • 24,797
  • 14
  • 106
  • 155
Ranjith
  • 31
  • 5
  • Have you tried anything? This is the first result in Google: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html – devrobf May 14 '13 at 11:50
  • possible duplicate of [JFileChooser Filters](http://stackoverflow.com/questions/13517770/jfilechooser-filters) – Andrew Thompson May 14 '13 at 11:52
  • Check my edit. After some careful reading I think this is what OP wants. – christopher May 14 '13 at 11:52
  • i dont want file chooser.. I need that Dialog box contain only installed program in the os.. – Ranjith May 14 '13 at 11:53
  • @user2364985 u have to implement it by yourself . see my answer i provided links for doing such on windows – qwr May 14 '13 at 12:12

3 Answers3

3

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);
        }
   } ...
}
aran
  • 10,978
  • 5
  • 39
  • 69
  • i dont want file chooser.. I need that Dialog box contain only installed program in the os – Ranjith May 14 '13 at 11:54
  • @user2364985 You can configure your FileChooser with ExtensionFilter: http://docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html – Vairis May 14 '13 at 12:03
3

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

To my knowledge, there is nothing in the J2SE like that. OTOH the Desktop API can open a File in whatever app. is the default consumer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You can make your own dialog for that purpose .And for coming how to get program list .on windows you can use registry . see this link Detecting installed programs via registry

and also check how to acces registry via java read/write to Windows Registry using Java

Community
  • 1
  • 1
qwr
  • 3,660
  • 17
  • 29