questo è il jframe:
così scansiono una directory e modello il jtree e stampo su una text area tutto il contenuto richiamando ListDir.sortList(dir);codice:public class Main extends javax.swing.JFrame { private DefaultMutableTreeNode master = new DefaultMutableTreeNode(); ...... private void itemChooseActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = null; StringBuilder lista = null; File dir = null; if (fc == null) { fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int stato = fc.showOpenDialog(fc); if (stato == JFileChooser.APPROVE_OPTION) { dir = fc.getSelectedFile(); try { lista = ListDir.sortList(dir); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } } listAllFiles(dir.getPath(), master, rootPaneCheckingEnabled); areaFile.setText(lista.toString()); } public static void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) { File[] children = new File(directory).listFiles(); Arrays.sort(children, new myComparator()); for (int i = 0; i < children.length; i++) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName()); if (children[i].isDirectory() && recursive) { parent.add(node); listAllFiles(children[i].getPath(), node, recursive); } else if (!children[i].isDirectory()) { parent.add(node); } } } ......
vorrei però fare in modo che quando clicco su un nodo nella textarea venga stampato il contenuto di quella directory.codice:public class ListDir { private static void listingDir(File dir, int livello, StringBuilder sb) { String spazio = ""; File[] elementi = dir.listFiles(); for (int l = 0; l < livello; l++) { spazio += " |_"; } Arrays.sort(elementi, new myComparator()); for (File files : elementi) { sb.append(spazio).append(files.getName()).append("\n"); if (files.isDirectory()) { listingDir(files, livello + 1, sb); } } } public static StringBuilder sortList(File dir) throws FileNotFoundException { validateDir(dir); StringBuilder sb = new StringBuilder(); listingDir(dir, 0, sb); return sb; } private static void validateDir(File dir) throws FileNotFoundException { if (dir == null) { throw new IllegalArgumentException("Directory should not be null."); } if (!dir.exists()) { throw new FileNotFoundException("Directory does not exist: " + dir); } if (!dir.isDirectory()) { throw new IllegalArgumentException("Is not a directory: " + dir); } if (!dir.canRead()) { throw new IllegalArgumentException("Directory cannot be read: " + dir); } } }
1-che evento mi conviene usare sul jtree?
2-come faccio a recuperare il percorso del nodo scelto??

Rispondi quotando

