1

I have a JList holding objects of type

Result(String title, String content, String filePath)

This JList has a MouseListener. I would like to implement a double clicked MouseEvent that passess the selected result's filePath, so it can open the File outside of my Java GUI application.

For Example:

If I double click a Result object in the JList with title: "Document1" content: "This is Document1" filePath: "C:\doc1.doc"

I would like the program to open this document outside of the application in Microsoft Word.

In otherwords, how can I bypass JFileChooser and open a File outside of my application in its default application?

tshepang
  • 11,360
  • 21
  • 88
  • 132
Brian
  • 6,760
  • 15
  • 53
  • 72

2 Answers2

2

I think you'r looking for evt.getClickCount()
Inside your mouseEvent method you can create a control statement like this:

public void mouseClicked(MouseEvent ev){
 if(ev.getClickCount() ==2){
  try{
  java.awt.Desktop.getDesktop().open(new File("path/to/file"));
}catch(FileNotFoundException ex){
//.....
}
}
}

Also check this link .

Community
  • 1
  • 1
Azad
  • 5,037
  • 19
  • 38
  • 1
    Thank you for this answer. I especially appreciated how you put it in the context of the mouseClicked method. – Brian Jun 07 '13 at 21:12
1

Try this:

Desktop.getDesktop().open(new File("filePath"));

i.e.

Desktop.getDesktop().open(new File("C:/doc1.doc"));

It should open the file with the default application

andred
  • 1,177
  • 1
  • 16
  • 28
BackSlash
  • 21,187
  • 21
  • 84
  • 132
  • Thanks for the prompt response. The only thing is the open method requires a File parameter and not a String. – Brian Jun 07 '13 at 21:11