Ola galera.
Talves eu possa estar fazendo um monte de besteiras…
mas é o seguinte. To querendo implementar uma Jtree que pegue desde da raiz do sistema.
Fiz um metodo que deveria ser chamado recursivamente. Detalhe, ainda to começando com java e nao sei bem se é assim.
Mas quando adiciono a recursão dá erro.
segue o codigo
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class tela {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="10,10"
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTree jTree = null;
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(4, 4, 177, 458));
jScrollPane.setViewportView(getJTree());
}
return jScrollPane;
}
/**
* This method initializes jTree
*
* @return javax.swing.JTree
*/
private JTree getJTree() {
if (jTree == null) {
File raizes[]= File.listRoots();
DefaultMutableTreeNode raiz = new DefaultMutableTreeNode(raizes[0]);
File nos[] = raizes[0].listFiles();
Arrays.sort(nos);
for(int i=0;i<nos.length;i++)
{
DefaultMutableTreeNode no= new DefaultMutableTreeNode(nos[i].getName());
if(nos[i].canRead()){
if(nos[i].isDirectory())
{
no=dir(nos[i]);
//no= new DefaultMutableTreeNode(nos[i].getName());
}
raiz.add(no);
}
}
jTree = new JTree(raiz);
}
return jTree;
}
public DefaultMutableTreeNode dir(File arq)
{
DefaultMutableTreeNode no = new DefaultMutableTreeNode(arq.getName());
File files[]=arq.listFiles();/*new FileFilter(){
public boolean accept(File dir) {
return dir.isFile();}
});*/
for(int i=0;i<files.length;i++)
{
if(files[i].canRead())
{
//if(files[i].isHidden()){
if(files[i].isFile()){
no.add(new DefaultMutableTreeNode(files[i].getName()));
}
else
{ if(files[i].isDirectory())
no.add(dir(files[i]));
}
//}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tela application = new tela();
application.getJFrame().setVisible(true);
}
});
}
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(617, 495);
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Application");
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
}
valeu pessoal. :lol: