简体   繁体   中英

Open up windows explorer in java

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?

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:\\"));

Edit

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"));
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM