Pessoal, estou tentando fazer ler o diretório qualquer que entra dentro dos sub diretório.
Tenho todo código pronto, está até funcionando, mas para fazer ler sub diretório acho que tenho que criar uma recursividade, tenho o conceito ± recursividade, assim e tranquilo fazer um método repercussivo de fatorial, soma, essas coisas, mas não estou conseguindo assimilar com o JFileChooser. Quem se habilita a dar um help?
veja meu método.
protected void do_btnDiretorio_actionPerformed( ActionEvent arg0 ) {
// Instanciando Diretorio
JFileChooser abrir = new JFileChooser();
// Selecionando maias de um arquivo
abrir.setMultiSelectionEnabled( false );
// restringe a amostra a diretorios apenas
abrir.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
// Abrindo diretorio
abrir.showOpenDialog( this );
try
{
File diretorio = abrir.getSelectedFile();
if( diretorio.exists() )
{
if( diretorio.isDirectory() )
{
// Armazena o nome do Diretorios em um Array de String
String[] arrayDiretorio = diretorio.list();
for( String diretorioNome : arrayDiretorio )
{
textAreaSelecao.append( diretorioNome );
textAreaSelecao.append( "\n" );
}
}
}
else
{
JOptionPane.showMessageDialog( null, "Diretorio nao existe" );
}
}
catch ( Exception erro )
{
}
}
Minha classe completa. Estou usando o Windowbuilder
package gui;
import java.awt.BorderLayout;
public class JFileChose extends JFrame implements ActionListener {
private JPanel contentPane;
private JButton btnDiretorio;
private JScrollPane scrollPane;
private JTextArea textAreaSelecao;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFileChose frame = new JFileChose();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFileChose() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(this.contentPane);
this.btnDiretorio = new JButton("Abrir Diretorio");
btnDiretorio.addActionListener(this);
scrollPane = new JScrollPane();
GroupLayout gl_contentPane = new GroupLayout(this.contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
.addComponent(btnDiretorio))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnDiretorio)
.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE)
.addGap(32))
);
textAreaSelecao = new JTextArea();
scrollPane.setViewportView(textAreaSelecao);
this.contentPane.setLayout(gl_contentPane);
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == btnDiretorio) {
do_btnDiretorio_actionPerformed(arg0);
}
}
protected void do_btnDiretorio_actionPerformed( ActionEvent arg0 ) {
// Instanciando Diretorio
JFileChooser abrir = new JFileChooser();
// Selecionando maias de um arquivo
abrir.setMultiSelectionEnabled( false );
// restringe a amostra a diretorios apenas
abrir.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
// Abrindo diretorio
abrir.showOpenDialog( this );
try
{
File diretorio = abrir.getSelectedFile();
if( diretorio.exists() )
{
if( diretorio.isDirectory() )
{
// Armazena o nome do Diretorios em um Array de String
String[] arrayDiretorio = diretorio.list();
for( String diretorioNome : arrayDiretorio )
{
textAreaSelecao.append( diretorioNome );
textAreaSelecao.append( "\n" );
}
}
}
else
{
JOptionPane.showMessageDialog( null, "Diretorio nao existe" );
}
}
catch ( Exception erro )
{
}
}
}