I want to use the file chooser of the operating system from java.
I am currently using JFileChooser but that looks ancient!
I saw this question : How to use the default File Chooser for the operating system? 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!)
Like i want to only allow user to select directories but not quite possible in FileDialog(or i don't know it)
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 :
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;
}
}
}
}