|
Buscador
Secciones
Otras zonas
Registro
Foros
Recomendamos
|
Abajo tenemos el código TreeDemo.java que implementa el árbol del ejemplo anterior.
public TreeDemo() {
...
//Crea los nodos.
DefaultMutableTreeNode top = new
DefaultMutableTreeNode("The Java Series");
createNodes(top);
//Crea un árbol que permite una selección a la vez.
JTree tree = new JTree(top);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Escucha cuando cambia la selección.
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
(e.getPath().getLastPathComponent());
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
BookInfo book = (BookInfo)nodeInfo;
displayURL(book.bookURL);
} else {
displayURL(helpURL);
}
}
});
//Crea el scroll pane y le añade el árbol.
JScrollPane treeView = new JScrollPane(tree);
...
//Añade los scroll panes a un split pane.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
treeView, htmlView);
...
}
private class BookInfo {
public String bookName;
public URL bookURL;
public BookInfo(String book, String filename) {
bookName = book;
./Set bookURL...
}
//Esto es lo que se mostrará en el árbol.
public String toString() {
return bookName;
}
}
...
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode book = null;
category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);
//Tutorial
book = new DefaultMutableTreeNode(new BookInfo
("The Java Tutorial: Object-Oriented Programming for the Internet",
"tutorial.html"));
category.add(book);
...
category = new DefaultMutableTreeNode("Books for Java Implementers");
top.add(category);
//VM
book = new DefaultMutableTreeNode(new BookInfo
("The Java Virtual Machine Specification",
"vm.html"));
category.add(book);
//Language Spec
book = new DefaultMutableTreeNode(new BookInfo
("The Java Language Specification",
"jls.html"));
category.add(book);
}
|
Utilidades
Patrocinados |
Copyright © 1999-2007
Programación en castellano.
Todos los derechos reservados.
Formulario de Contacto -
Datos legales -
Publicidad
Hospedaje web y servidores dedicados linux por Ferca Network
red internet: musica mp3 | logos y melodias | hospedaje web linux | registro de dominios | servidores dedicados
más internet: comprar | recursos gratis | posicionamiento en buscadores | tienda virtual | gifs animados