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?

1
  • Over 3,000 views and no up votes :( Commented Nov 26, 2014 at 16:57

2 Answers 2

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 .

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this answer. I especially appreciated how you put it in the context of the mouseClicked method.
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

1 Comment

Thanks for the prompt response. The only thing is the open method requires a File parameter and not a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.