Download presentation
Presentation is loading. Please wait.
Published byRandolph Todd Modified over 9 years ago
1
More Swing utilities: JTree COMP204, Bernhard Pfahringer Code snippets taken from Sun’s JTree demo example
2
Displaying/inspecting/modified hierarchical data
3
Jtree displays tree nodes // node data structure: DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); createNodes(top); JTree tree = new JTree(top); tree.addTreeSelectionListener(this);
4
TreeSelectionListener public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) return; Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { BookInfo book = (BookInfo)nodeInfo; displayURL(book.bookURL); } else { displayURL(helpURL); }
5
Build up data structure private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category = null; DefaultMutableTreeNode book = null; category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); book = new DefaultMutableTreeNode(new BookInfo ("The Java Tutorial: A Short Course on the Basics", "tutorial.html")); category.add(book);
6
Panel nesting TreeDemo extends Jpanel JScrollPane treeView = new JScrollPane(tree); htmlPane = new JEditorPane(); htmlPane.setEditable(false); JScrollPane htmlView = new JScrollPane(htmlPane); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(treeView); splitPane.setBottomComponent(htmlView); splitPane.setDividerLocation(100); add(splitPane);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.