[英]How to use the default File Chooser for the operating system in Java (in Linux and Windows)
I want to use the file chooser of the operating system from java.我想使用 java 操作系统的文件选择器。
I am currently using JFileChooser but that looks ancient!我目前正在使用 JFileChooser 但它看起来很古老!
I saw this question: How to use the default File Chooser for the operating system?我看到了这个问题: 如何使用操作系统的默认文件选择器? java java
But there are problems with that!但是这样做有问题!
I have tried:我努力了:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex) {
ex.printStackTrace();
}
No difference!没有不同!
I tried using JFileDialog but firstly as far as i have read on the internet it is better to avoid awt until you have a very good reason to use it(according to https://stackoverflow.com/a/21279145/14911094 ) and also i feel some features that JFileChooser has awt hasn't(or i am not aware of it yet!)我尝试使用 JFileDialog 但首先就我在互联网上阅读的内容而言,最好避免使用 awt,直到您有充分的理由使用它(根据https://stackoverflow.com/a/21279145/14911094 )以及我觉得 JFileChooser 的一些功能 awt 还没有(或者我还不知道!)
Like i want to only allow user to select directories but not quite possible in FileDialog(or i don't know it)就像我只想允许用户使用 select 目录但在 FileDialog 中不太可能(或者我不知道)
And moreover the question i linked is quite old so i guess there is a new way to do it now?而且我链接的问题已经很老了,所以我想现在有一种新的方法可以做到吗?
I current am using JFileChooser here is a piece of code that will help you reproduce what i have now:我目前正在使用 JFileChooser 这是一段代码,可以帮助您重现我现在拥有的内容:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Try{
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose directory");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.addActionListener(new MyActionListener());
fileChooser.showOpenDialog(new JFrame());
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
switch (actionEvent.getActionCommand()){
case "ApproveSelection":{
// DO SOMETHING
break;
}
case "CancelSelection":{
// DO SOMETHING
break;
}
}
}
}
So, I could not doscover why is setLookAndFeel is not working for me!所以,我不知道为什么 setLookAndFeel 不适合我!
But i have got a solution to the problem!但我有解决问题的办法!
So, here is the code:所以,这里是代码:
import java.util.*;
import java.io.*;
import java.awt.*;
public class Try2{
public static void main(String[] args) throws Exception{
Process p = Runtime.getRuntime().exec("zenity --file-selection --directory");
Scanner sc = new Scanner(p.getInputStream());
System.out.println("-----------------------------------");
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
System.out.println("-----------------------------------");
}
}
This is the closest to what i want这是最接近我想要的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.