1 / 17

JTree

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

ratana
Télécharger la présentation

JTree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JTree javax.swing.tree javax.swing.event

  2. ComponentsJTree 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().getLastPathComponent());

  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.getLastSelectedPathComponent();

More Related