More Swing utilities: JTree COMP204, Bernhard Pfahringer Code snippets taken from Sun’s JTree demo...
-
Upload
randolph-todd -
Category
Documents
-
view
212 -
download
0
description
Transcript of More Swing utilities: JTree COMP204, Bernhard Pfahringer Code snippets taken from Sun’s JTree demo...
More Swing utilities: JTree
COMP204, Bernhard Pfahringer
Code snippets taken from Sun’s JTree demo example
Displaying/inspecting/modifiedhierarchical data
Jtree displays tree nodes// node data structure:
DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java
Series");createNodes(top);
JTree tree = new JTree(top);
tree.addTreeSelectionListener(this);
TreeSelectionListenerpublic 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); }}
Build up data structureprivate 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);
Panel nestingTreeDemo 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);