You are on page 1of 2

javax.

swing
Class JTree
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JTree
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

public class JTree


extends JComponent
implements Scrollable, Accessible

Un control que despliega un juego de data jerarquica comA control that displays a set of
hierarchical data as an outline. Usted puede encontrar documentacion practica y
ejemplos del uso de arboles en How to Use Trees, a section in The Java Tutorial.

Un especifico nodo en un arbol puede ser identificado por un A specific node in a tree
can be identified either by a TreePath (an object that encapsulates a node and all of its
ancestors), or by its display row, where each row in the display area displays one node.
An expanded node es un nodo no hoja()is a non-leaf node (as identified by
TreeModel.isLeaf(node) returning false) that will displays its children when all its
ancestors are expanded. A collapsed node is one which hides them. A hidden node es
aquel que esta por debajo de un ancestro colapsado. All of a viewable nodes parents are
expanded, but may or may not be displayed. A displayed node is both viewable and in
the display area, where it can be seen.

The following JTree methods use "visible" to mean "displayed":

 isRootVisible()
 setRootVisible()
 scrollPathToVisible()
 scrollRowToVisible()
 getVisibleRowCount()
 setVisibleRowCount()

The next group of JTree methods use "visible" to mean "viewable" (under an expanded
parent):

 isVisible()
 makeVisible()

If you are interested in knowing when the selection changes implement the
TreeSelectionListener interface and add the instance using the method
addTreeSelectionListener. valueChanged will be invoked when the selection
changes, that is if the user clicks twice on the same node valueChanged will only be
invoked once.
If you are interested in detecting either double-click events or when a user clicks on a
node, regardless of whether or not it was selected, we recommend you do the following:

final JTree tree = ...;

MouseListener ml = new MouseAdapter() {


public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(),
e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
mySingleClick(selRow, selPath);
}
else if(e.getClickCount() == 2) {
myDoubleClick(selRow, selPath);
}
}
}
};
tree.addMouseListener(ml);

NOTE: This example obtains both the path and row, but you only need to get the one
you're interested in.

To use JTree to display compound nodes (for example, nodes containing both a graphic
icon and text), subclass TreeCellRenderer and use
setCellRenderer(javax.swing.tree.TreeCellRenderer) to tell the tree to use it.
To edit such nodes, subclass TreeCellEditor and use
setCellEditor(javax.swing.tree.TreeCellEditor).

Like all JComponent classes, you can use InputMap and ActionMap to associate an
Action object with a KeyStroke and execute the action under specified conditions.

Warning: Serialized objects of this class will not be compatible with future Swing
releases. The current serialization support is appropriate for short term storage or RMI
between applications running the same version of Swing. As of 1.4, support for long
term storage of all JavaBeans has been added to the java.beans package. Please see
TM

XMLEncoder.

You might also like