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

3

2

I have made a button, but I don't now how to make it open a specific directory like %appdata% when the button is clicked on.

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 make 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
        }
Obsessive answered 10/6, 2012 at 7:20 Comment(6)
Do you want to open the file selector, or the system file explorer?Tirol
I want to open in system file explorerObsessive
I wrote a FileExplorer class for that purpose around a month ago. I also posted a more detailed answer.Tirol
@Vulcan At just over 50 lines of well formatted, commented code, that is short enough to include in an answer. I don't think your approach is optimal for any 1.6+ (Use Desktop class) JRE, but if the OP needs support for pre 1.6..Lello
@AndrewThompson It is included in an answer as well, but thanks for pointing that out. I forgot the question was to reveal a directory rather than a specific file, which is what my code is designed to do (Desktop#open executes files with related applications, and directories with file explorers).Tirol
Why did you create a duplicate post?Tomas
L
4
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);
    }
}
Lello answered 10/6, 2012 at 8:28 Comment(1)
I prefer to use the 2nd File constructor shown in the edit, though it matters little if this is only intended for Windows. Otherwise, either find an environment variable for that platform, or offer a file chooser.Lello
T
1

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

Windows: Explorer /select, file

Mac: open -R file

Linux: xdg-open file

I wrote a FileExplorer class for the purpose of revealing files in the native file explorer, but you'll need to edit it to detect operating system. http://textu.be/6

NOTE: This is if you wish to reveal individual files. To reveal directories, Desktop#open(File) is far simpler, as posted by Andrew Thompson.

Tirol answered 10/6, 2012 at 8:29 Comment(1)
Your code from textu.be/6 is no longer available. Consider reposting it on another host or including it in your answer (or just remove informations about it from your answer).Shepp
C
0

If you are using Windows Vista and higher, System.getenv("APPDATA"); will return you C:\Users\(username}\AppData\Roaming, so you should go one time up, and use this path for filechooser, Just a simple modified Andrew's example,

    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

Coopt answered 10/6, 2012 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.