[英]How to make the 'open with' dialog box?
我編寫了一個程序來搜索.txt文件。
如果單擊文件,則意味着將出現“打開方式”對話框,並且該對話框將包含所有已安裝程序的列表。
我正在使用以下代碼搜索文件:
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");
}
我可以使用什么Java代碼來顯示“打開方式”對話框?
您應該為此使用FileChooser
。 在這里看看:
//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);
}
} ...
}
我可以使用什么Java代碼來顯示“打開方式”對話框?
據我所知,J2SE中沒有類似的東西。 OTOH Desktop
API可以在任何應用程序中打開File
。 是默認使用者。
您可以為此創建自己的對話框。要獲取如何在Windows上獲取程序列表,可以使用Registry。 看到此鏈接通過注冊表檢測已安裝的程序
並檢查如何通過Java 使用Java讀寫Windows注冊表來訪問注冊表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.