Open a folder on your computer with JAVA

0

In the following code I am creating a class which creates a folder in the computer and also adds a jPanel with different options, what happens is that when you press the START button you should open the folder, but I tried several ways without getting results.

public class Carpeta extends JPanel implements ActionListener {
    private String direccion = "C:\Users\capri\Privados\";
    private String user;
    private String nombre;
    private JLabel etiqueta;
    private JCheckBox ocultar;
    private JCheckBox visible;
    private JButton start;

    public Carpeta(String user, String nombre, String estados) {
        setSize(400,100);
        this.setLayout(new GridLayout(1,5));
        this.etiqueta = new JLabel(nombre);
        this.user = user;
        this.nombre = nombre;
        ocultar = new JCheckBox("OCULTO");
        visible = new JCheckBox("VISIBLE");
        if(estados.charAt(0)==1){
            ocultar.setSelected(true);
        }
        if(estados.charAt(1)==1){
            visible.setSelected(true);
        }
        start = new JButton("INICIAR");
       start.addActionListener(this);
        visible.addActionListener(this);
        ocultar.addActionListener(this);

        add(etiqueta);
        add(new JSeparator());
        add(start);
        add(ocultar);
        add(visible);

        this.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        Object ejec =  e.getSource();

        ***en esta parte****************************************
        if(ejec == start){
            String urlm = direccion+nombre;
            ProcessBuilder p  = new ProcessBuilder();
            p.command("cmd.exe ","\c",urlm);
        }   
       *************************************************************
       if(visible.isSelected()){
           try{
               Runtime.getRuntime().exec("attrib -s -h "+direccion+nombre);
               }catch(IOException ex){}
           ocultar.setSelected(false);
       }else{
           ocultar.setSelected(true);
       }
       if(ocultar.isSelected()){
            try{
                Runtime.getRuntime().exec("attrib +s +h "+direccion+nombre);
                }catch(IOException ex){}
            visible.setSelected(false);
        }else{
            visible.setSelected(true);
        }
    }
}

I have also used the following, but even so I can not open the folder

    if(ejec == start){
        try{
            Runtime.getRuntime().exec("start "+direccion+nombre);
            }catch(IOException ex){}
    }   

if someone could help me and at the same time explain to me what is the reason why my code does not work.

    
asked by Nick JAG 20.12.2018 в 00:15
source

1 answer

0

As you express in the comments the problem you are experiencing is that the Process does not start (it is not called p.start() ) as # 1, also to call a Folder you should use something like this: Runtime.getRuntime().exec("cmd /c start "+direccion); but in any case, it is better to use Desktop.getDesktop().open(new File("C:\folder"));

BUT! In addition given what you try to do with the application I will include some recommendations to make this application is not dependent on Windows commands:

//imports relevantes: 
import java.awt.Desktop;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributeView;

//...
//codigo del UI (no hay cambios con respecto al codigo provisto)  
//...

@Override
public void actionPerformed(ActionEvent e) {
    Object ejec = e.getSource();
    Path path = Paths.get(direccion,nombre);
    if (ejec == start) {
        showfolder(path);
    }
    flipSwithes();
    cambiarAttributo(path,visible.isSelected());
}

/**
 * metodo que despliega el folder/archivo
 * @param path el folder/archivo a desplegar
 */
private void showfolder(Path path) {
    if (!Files.exists(path)) {
        System.out.printf("el archivo < %s > no existe", path.toString());
    } else {
        try {
            Desktop.getDesktop().open(path.toFile());
        } catch (IOException ex) {
            Logger.getLogger(Carpeta.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

/**
 * cambia el attributo de visibilidad del archivo/folder 
 * @param path direccion del archivo/folder
 * @param visible si debe o no ser visible
 */
private void cambiarAttributo(Path path,boolean visible) {
    DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class);
        try {
            if (dosView.readAttributes().isHidden()!=!visible) {
                dosView.setHidden(!visible);
            }
        } catch (IOException ex) {
            Logger.getLogger(Carpeta.class.getName()).log(Level.SEVERE, null, ex);
        }
}

/**
 * configura los checkboxes de visible/ocultar.
 */
private void flipSwithes() {
     if (visible.isSelected()) {
        ocultar.setSelected(false);
    } else {
        ocultar.setSelected(true);
    }
    if (ocultar.isSelected()) {
        visible.setSelected(false);
    } else {
        visible.setSelected(true);
    }
}

and at this point we do not need to execute anything outside of Java code. :)

Disk Operating System FileAttributeView javadoc

how to use Disk Operating System FileAttributeView

using Desktop Class

    
answered by 20.12.2018 / 01:56
source