简体   繁体   English

在不使用JFileChooser的情况下从Java GUI打开默认应用程序中的文件

[英]Open a file in default application from Java GUI without using JFileChooser

I have a JList holding objects of type 我有一个JList持有类型的对象

Result(String title, String content, String filePath)

This JList has a MouseListener. 这个JList有一个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. 我想实现一个双击的MouseEvent来传递选定结果的filePath,这样它就可以在我的Java GUI应用程序之外打开File。

For Example: 例如:

If I double click a Result object in the JList with title: "Document1" content: "This is Document1" filePath: "C:\\doc1.doc" 如果我双击JList中带有标题的结果对象:“Document1”内容:“这是Document1”filePath:“C:\\ doc1.doc”

I would like the program to open this document outside of the application in Microsoft Word. 我希望程序在Microsoft Word中的应用程序之外打开此文档。

In otherwords, how can I bypass JFileChooser and open a File outside of my application in its default application? 换句话说,如何绕过JFileChooser并在其默认应用程序中打开我的应用程序之外的文件?

I think you'r looking for evt.getClickCount() 我想你在寻找evt.getClickCount()
Inside your mouseEvent method you can create a control statement like this: mouseEvent方法中,您可以创建一个这样的控制语句:

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 . 另请查看此链接。

Try this: 尝试这个:

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

ie

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

It should open the file with the default application 它应该使用默认应用程序打开文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM