JTree

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 9 μέρες)

81 εμφανίσεις

JTree

javax.swing.tree

javax.swing.event

Components

JTree and JTable


COMPONENT


Model


Selection Model


Editor


Renderer


JTree


Tree model


describes a JTree’s underlying data model. It
specifies how a tree is mapped over a data
structure.


Tree selection


Tree cell editor


Tree cell renderer

Parts of trees


Root


Parent


Node


Branch


Etc.

JTree


The JTree class displays
hierarchical data.


It displays the data
vertically.


Each row has one item,
called a “node.”

DefaultTreeModel


Is a simple implementation of Tree Model that
uses TreeNodes objects to hold items in the
tree.



TreeModel


DefaultTreeModel


TreeNode


DefaultMutableTreeNode

TreeModel methods


getChild(Object parent, int index)


getChildCount (Object parent)


getIndexOfChild(Object parent, Object child)


getRoot()


isLeaf(Object node)

DefaultMutableTreeNode


There is a rich set of methods in the
DefaultMutableTreeNode class for viewing and
manipulating nodes in a tree.

DefaultMutableTreeNode methods
Tree enumeration


preorderEnumeration()


Conducts a preorder traversal rooted at this node
and returns an enumeration of TreeNode objects


postorderEnumeration()


Conducts a postorder traversal rooted at this node
and returns and enumeration of TreeNode objects

JTree sample code


DefaultMutableTreeNode root =

new

DefaultMutableTreeNode("Root");


DefaultMutableTreeNode subroot = new
DefaultMutableTreeNode("Subroot");


DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");



DefaultMutableTreeNode leaf2 = new
DefaultMutableTreeNode("Leaf 2");



Quick JTree code


treeModel = new DefaultTreeModel(root);


tree = new JTree(treeModel
);


treeModel.insertNodeInto(subroot, root, 0);




treeModel.insertNodeInto(leaf1, subroot, 0);



treeModel.insertNodeInto(leaf2, root, 1);


Customizing a Tree's Display


Java, Windows, and
Motif Look & Feel
implementations


Add icons to tree nodes

Dynamically Changing a Tree



tree = new JTree(treeModel);


tree.setEditable(true);



treeModel.addTreeModelListener(new

MyTreeModelListener());



...


class MyTreeModelListener implements TreeModelListener
{ public void treeNodesChanged(TreeModelEvent e) {


DefaultMutableTreeNode node;


node =

(DefaultMutableTreeNode)(e.getTreePath().getLastPa
thComponent());


Homework


Compile and run the following java programs:


TestTree.java


SampleTreeTest.java




Links to the code will be found off the class website or

http://my.fit.edu/~ghrezo/TestTree.java.htm

http://my.fit.edu/~ghrezo/SampleTreeTest.java.htm

Tree node selection

(tree listeners)


tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);



//Listen for when the selection changes.
tree.addTreeSelectionListener(new
TreeSelectionListener() {


public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node =

(DefaultMutableTreeNode)tree.getLastSelectedP
athComponent();