|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
JTree Java
Salve, sono riuscito a popolare un JTree con delle cartelle e dei file di un percorso del mio disco, ma non riesco a farmi restituire il file selezionato x poi aprirlo con il suo programma predefinito, qualcuno può aiutarmi a capire come fare? grazie mille.
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Per capire se l'utente vuole aprire il file associato al nodo oppure no, puoi usare un ascoltatore per l'evento "è cambiata la selezione corrente dell'albero" oppure un ascoltatore per l'evento doppio-click del mouse.
Di solito si usa il secondo. In questo caso dirai: Codice:
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() >= 2) {
TreePath path = getPathForLocation(e.x, e.y);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
File file = (File) node.getUserObject();
}
}
});
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
ma getPathForLocation cosa è un metodo o una classe? perchè netBeans non melo riconosce.
|
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
Mi lancia questa eccezzione: "FileTreeNode cannot be cast to javax.swing.tree.DefaultMutableTreeNode"
e dentro a getPathForLocation(e.getX, e.getY); invece di getPathForLocation(e.x, e.y); |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
DefaultMutableTreeNode è l'elemento standard di un JTree. Se usi un tipo diverso di nodo allora fai la conversione esplicita al tipo che sai di stare usando:
FileTreeNode node = (FileTreeNode) path.getLastPathComponent()
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#7 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
Ho provato con DefaultTreeCellRenderer e mi da lo stesso errore, mentre con
FileTreeNode non riesco a recuperare il file, nel senso che non so dopo cosa mettergli per apreire il file. |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
TreeCellRenderer serve per "disegnare" i nodi che è un'altra cosa.
Questo FileTreeNode da dove arriva? Ce l'avrà una classe. Guarda e vedi se ha un metodo getFile o qualcosa del genere.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#9 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
scusami, ma proprio non ci riesco, ti posto il codice e facciamo prima:
public class Albero { protected static FileSystemView fsv = FileSystemView.getFileSystemView(); private static class FileTreeCellRenderer extends DefaultTreeCellRenderer { private Map<String, Icon> iconCache = new HashMap<String, Icon>(); private Map<File, String> rootNameCache = new HashMap<File, String>(); @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { FileTreeNode ftn = (FileTreeNode) value; File file = ftn.file; String filename = ""; if (file != null) { if (ftn.isFileSystemRoot) { filename = this.rootNameCache.get(file); if (filename == null) { filename = fsv.getSystemDisplayName(file); this.rootNameCache.put(file, filename); } } else { filename = file.getName(); if (file.isFile()) { setFileDaAprire(file); } } } JLabel result = (JLabel) super.getTreeCellRendererComponent(tree, filename, sel, expanded, leaf, row, hasFocus); if (file != null) { Icon icon = this.iconCache.get(filename); if (icon == null) { icon = fsv.getSystemIcon(file); this.iconCache.put(filename, icon); } result.setIcon(icon); } return result; } } private static class FileTreeNode implements TreeNode { private File file; private File[] children; private TreeNode parent; private boolean isFileSystemRoot; public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) { this.file = file; this.isFileSystemRoot = isFileSystemRoot; this.parent = parent; this.children = this.file.listFiles(); if (this.children == null) { this.children = new File[0]; } } public FileTreeNode(File[] children) { this.file = null; this.parent = null; this.children = children; } @Override public Enumeration<?> children() { final int elementCount = this.children.length; return new Enumeration<File>() { int count = 0; @Override public boolean hasMoreElements() { return this.count < elementCount; } @Override public File nextElement() { if (this.count < elementCount) { return FileTreeNode.this.children[this.count++]; } throw new NoSuchElementException("Vector Enumeration"); } }; } @Override public boolean getAllowsChildren() { return true; } @Override public TreeNode getChildAt(int childIndex) { return new FileTreeNode(this.children[childIndex], this.parent == null, this); } @Override public int getChildCount() { return this.children.length; } @Override public int getIndex(TreeNode node) { FileTreeNode ftn = (FileTreeNode) node; for (int i = 0; i < this.children.length; i++) { if (ftn.file.equals(this.children[i])) { return i; } } return -1; } @Override public TreeNode getParent() { return this.parent; } @Override public boolean isLeaf() { return (this.getChildCount() == 0); } } } |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Aggiungi a FileTreeNode un metodo:
public File getFile() { return file; } e sei a posto.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#11 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
E poi? come e dove lo richiamo getFile()?
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Nel mouse listener:
Codice:
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() >= 2) {
TreePath path = getPathForLocation(e.x, e.y);
FileTreeNode node = (FileTreeNode ) path.getLastPathComponent();
File file = node.getFile();
...fai quel che vuoi con file
}
}
});
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#13 |
|
Junior Member
Iscritto dal: Mar 2012
Messaggi: 7
|
Siii!!! grazie infinite ci sono riuscito.
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:26.


















