How to make a button that, when clicked, opens the% appdata% directory?

I made a button, but now I don’t know how to make it open a specific directory, for example %appdata% , when the button is clicked.

Here is the code β†’

 //---- button4 ---- button4.setText("Texture Packs"); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser=new JFileChooser("%appdata%"); int status = fileChooser.showOpenDialog(this); fileChooser.setMultiSelectionEnabled(false); if(status == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // do something on the selected file. } } 

And I want to do something like this β†’

 private void button4MouseClicked(MouseEvent e) throws IOException { open folder %appdata% // Open the folder in the file explorer not in Java. // When I click on the button, the folder is viewed with the file explorer on the screen } 
+2
java eclipse directory jframe
Jun 10 '12 at 7:20
source share
3 answers
 import java.awt.Desktop; import java.io.File; public class OpenAppData { public static void main(String[] args) throws Exception { // Horribly platform specific. String appData = System.getenv("APPDATA"); File appDataDir = new File(appData); // Get a sub-directory named 'texture' File textureDir = new File(appDataDir, "texture"); Desktop.getDesktop().open(textureDir); } } 
+3
Jun 10 '12 at 8:28
source share

Run the command using Runtime.exec (..). However, not every OS has the same file explorer, so you need to process the OS.

Windows: Explorer /select, file

Mac: open -R file

Linux: xdg-open file

I wrote the FileExplorer class to identify files in my own file explorer, but you will need to edit it to detect the operating system. http://textu.be/6

NOTE. This means that you want to open individual files. To show directories, Desktop#open(File) much simpler, as Andrew Thompson wrote.

+1
Jun 10 '12 at 8:29
source share

If you are using Windows Vista or higher, System.getenv("APPDATA"); will return you C:\Users\(username}\AppData\Roaming , so you have to go once and use this path for filechooser , Just a simple modified example of Andrew,

  String appData = System.getenv("APPDATA"); File appDataDir = new File(appData); // TODO: this path should be changed! JFileChooser fileChooser = new JFileChooser(appData); fileChooser.showOpenDialog(new JFrame()); 

More about windows xp and windows vista / 7/8

0
Jun 10 '12 at 10:00
source share



All Articles