Using this in Java will open Windows Explorer to the C drive:
Desktop.getDesktop().open(new File("c:\\"));
However, I also need the "Open File" functions highlighted here: https://i.sstatic.net/VNtMj.jpg
Is there a way to implement this in Java (using Windows Explorer, not Swing's FileChooser)?
Have a look at using JFileChooser while using the native system's look & feel:
public class NativeOpenDialogDemo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JFrame frame = new JFrame("Open File Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JButton openButton = new JButton("Open");
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
// do something
}
}
});
frame.add(openButton);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
}
}
We can use JFileChoose,
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file == null) {
return;
}
String fileName = chooser.getSelectedFile().getAbsolutePath();
......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With