Вы находитесь на странице: 1из 3

public class Arboles { public Arboles() { System.out.println("Un rbol genrico"); } public Arboles(String tipo) { System.out.

println("Un rbol tipo " + tipo); } public Arboles(int altura) { System.out.println("Un rbol de " + altura + " metros"); } public Arboles(int altura,String tipo) { System.out.println("Un " + tipo + " de " + altura + " metros"); } public static void Arboles arbol1 Arboles arbol2 Arboles arbol3 Arboles arbol4 } main(String args[]) { = new Arboles(4); = new Arboles("Roble"); = new Arboles(); = new Arboles(5,"Pino");

} /---------------------------------------------------------------/ Para crear un rbol se tiene que crear una raz que es el nodo principal del cual se van a colocar los dems nodos. public Arbol() { ... top = new DefaultMutableTreeNode("Raz"); createNodes(top); Crea un rbol que permite una seleccin a la vez. JTree tree = new JTree(treeModel); tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); Se incluye para las hojas la imagen "right.gif" DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setLeafIcon(new ImageIcon("./right.gif")); tree.setCellRenderer(renderer); Aqu se se crean los nodos del inicio del programa con sus respectivos programas private TreeNode createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category = null; DefaultMutableTreeNode book = null; DefaultMutableTreeNode sub = null; category = new DefaultMutableTreeNode("Arboles"); top.add(category); book = new DefaultMutableTreeNode("Nodos"); top.add(book); ....

En este momento ya tenemos creado el rbol con sus respectivos nodos /-------------------------------------------------------------------/ Este evento asociado a JTree nos permite prevenir que un nodo determinado se exp anda o colapse Como se muestra en la figura se abre una ventana en donde se puede prevenir la e xpansin del nodo. Object[] willExpandOptions = {"Cancelar Expansin", "Expandir"}; String willExpandText = "Un nodo de la rama va ha expandirse.\n" + "Click \"Cancelar Expancin\" Para prevenirlo."; String willExpandTitle = "Tree Will Expand"; Estas lineas de comando permiten que el evento se lleve a cabo /-----------------------------------------------------------------/ Este evento es el que permite crear mas nodos(adicionar), borrar todos los nodos (limpiar), o borrar solo un nodo(remover) Para adicionar se debe indicar que el seleccionado va ha ser el que tendr un hijo nodo y que va a tener como nombre Nuevo nodo y el numero al que corresponde public void actionPerformed(ActionEvent e) { //addObject("New Node " + newNodeSuffix++); Object child=("Nuevo Nodo " + newNodeSuffix++); DefaultMutableTreeNode parent = null; TreePath parentPath = tree.getSelectionPath(); if (parentPath == null) { parent = top; } else { parent = (DefaultMutableTreeNode) (parentPath.getLastPathComponent()); } //addObject(parentNode, child, true); DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = top; } treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); Para remover se debe tener en cuenta el nodo seleccionado para remover nicamente public void actionPerformed(ActionEvent e) { TreePath currentSelection = tree.getSelectionPath(); if (currentSelection != null) { DefaultMutableTreeNode currentNode = (DefaultMutableTree Node) (currentSelection.getLastPathComponent()); MutableTreeNode parent = (MutableTreeNode)(currentNode.g etParent());

if (parent != null) { treeModel.removeNodeFromParent(currentNode); return; } } Y para limpiar totalmente no se nesecita tener ningn nodo seleccionado el elimina ra todos menos la raz public void actionPerformed(ActionEvent e) { top.removeAllChildren(); treeModel.reload(); } /-----------------------------------------------------------------------/ Este evento permite que al seleccionar con el mouse el usuario seleccione un sol o nodo, o el nodo y el hijo. En nuestro ejemplo se selecciona un solo nodo (SING LE_TREE_SELECTION). tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); /------------------------------------------------------------------------/

Вам также может понравиться