Download presentation
Presentation is loading. Please wait.
Published byGervase Wilson Modified over 9 years ago
1
JTree javax.swing.tree javax.swing.event
2
Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer
3
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
4
Parts of trees Root Parent Node Branch Etc.
5
JTree The JTree class displays hierarchical data. It displays the data vertically. Each row has one item, called a “node.”
6
DefaultTreeModel Is a simple implementation of Tree Model that uses TreeNodes objects to hold items in the tree.
7
TreeModel – DefaultTreeModel TreeNode – DefaultMutableTreeNode
8
TreeModel methods getChild(Object parent, int index) getChildCount (Object parent) getIndexOfChild(Object parent, Object child) getRoot() isLeaf(Object node)
9
DefaultMutableTreeNode There is a rich set of methods in the DefaultMutableTreeNode class for viewing and manipulating nodes in a tree.
10
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
11
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");
12
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);
13
Customizing a Tree's Display Java, Windows, and Motif Look & Feel implementations
14
Add icons to tree nodes
15
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());
16
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
17
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();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.