Especificar "caminho" corretamente em JAVA [RESOLVIDO]

4 respostas
Spinkers

Amigos, quero que para cada botão abra determinada pasta, criei o evento para cada botão e tudo mais, mas mesmo alterando o caminho da pasta, sempre abre a mesma pasta "Documentos"
Alguém ajuda?

[color=red]Explique de forma bem detalhada por favor![/color]
(Estou usando JDialog)
(NetBeans)
[color=darkred]Lembrando que sou iniciante.[/color]

Veja a print de como o programa é:
[img]http://sphotos-g.ak.fbcdn.net/hphotos-ak-ash4/383476_148639795322964_2052353355_n.jpg[/img]

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Felipe
 */
public class MenuPastas extends javax.swing.JDialog {

    /**
     * Creates new form MenuPastas
     */
    public MenuPastas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Cambria Math", 1, 24)); // NOI18N
        jLabel1.setText("MENU DE PASTAS");

        jButton1.setText("Documentos");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Downloads");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Computador");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Imagens");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Musicas");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Videos");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("Jogos");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setText("Hardware");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Oculto");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Lixeira");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setText("Disco Local C");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setText("Desktop");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jButton13.setText("Bibliotecas");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        jButton14.setText("Painel CTRL");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton10, jButton11, jButton12, jButton13, jButton14, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton8)
                    .addComponent(jButton9)
                    .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton11)
                    .addComponent(jButton12)
                    .addComponent(jButton13)
                    .addComponent(jButton14))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton10, jButton11, jButton12, jButton13, jButton14, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {        
            Runtime.getRuntime().exec("explorer C:\\Documents Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Downloads Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Computer Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Image Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Music Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Videos Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Games Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Control Panel Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Library Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Desktop Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Dump Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Roaming Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
        try {        
            Runtime.getRuntime().exec("explorer C:\\Documents Files");
        } catch (IOException ex) {
            Logger.getLogger(MenuPastas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MenuPastas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MenuPastas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MenuPastas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MenuPastas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MenuPastas dialog = new MenuPastas(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}

4 Respostas

Leandro_M

Aponta sempre para o “Documentos” quando não encontra o path que você indica

Mas veja, assim você vai conseguir. Eu uso o Win 7

Runtime.getRuntime().exec("explorer C:\\Users\\<SEU USER>\\Music"); Runtime.getRuntime().exec("explorer C:\\Users\\<SEU USER>\\Pictures");

Spinkers

Certo, mas se eu colocar “Lucas” no USER e for testar em outro computador com nome Diferente vai dar problema né?
e então, como eu faço? existe algum codigo que funcione para qualquer User?

Leandro_M

Use a classe File para saber se a unidade é C: ou D: e o nome do user.

Lembre-se que sua lógica tem que atender versões diferentes de win, uma vez que cada um tempo um padrão diferente.

Flw

E

http://docs.oracle.com/javase/6/docs/api/javax/swing/filechooser/FileSystemView.html#getDefaultDirectory() lhe retorna o diretório de Documentos, mesmo que ele não esteja no drive C e sim em outro drive, e não depende se o Windows é XP, 7, Vista, 8, etc.

A partir do diretório de Documentos é possível achar os outros, que normalmente são subdiretórios desse diretório, ou então ficam em paralelo.

Criado 2 de junho de 2013
Ultima resposta 3 de jun. de 2013
Respostas 4
Participantes 3