I have been looking for an answer to this on Stack Overflow, but I couldn't find an answer that worked for me.
Using Java, how do I create a button that will launch an Explorer Window to a specified directory? If this is possible, how do I make it work for OSX and Linux?
To view the project explorer, click on Window menu then, click on Show View and select Project Explorer. There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.
File Explorer in Windows 11 helps you get the files you need quickly and easily. To check it out in Windows 11, select it on the taskbar or the Start menu, or press the Windows logo key + E on your keyboard.
To use Command Prompt to run File Explorer, use these steps: Open Start. Search for Command Prompt and click the top result to open the console. Type the following command and press Enter: explorer.
Java FileInputStream class is used to open and read a file. We can open and read a file by using the constructor of the FileInputStream class. The signature of the constructor is: public FileInputStream(File file) throws FileNotFoundException.
I am not sure how it works in other OS but in Windows you can use something like this
Desktop.getDesktop().open(new File("c:\\"));
Found another way (check link to FileExplorer class from that answer). Also you can use System.getProperty("os.name")
to determine operation system.
javax.swing.JButton myButton = new javax.swing.JButton("BUTTON TEXT");
myButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
java.awt.Desktop.getDesktop().open(new java.io.File("MY PATH NAME HERE"));
}
});
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