Article View: borland.public.jbuilder.java.swing
Article #9340Re: Building a JTree - Dynamically
From: Andreas Andersson
Date: Mon, 17 Apr 2000 00:00
Date: Mon, 17 Apr 2000 00:00
146 lines
4993 bytes
4993 bytes
Your question isn't very simple to answer just like that. But there is some
methods provided to dynamically change a tree:
- DefaultTreeModel class
* removeNodeFromParent(..)
* insertNodeInto(...)
* reload(...)
* setRoot(...)
- DefaultMutableTreeNode
* removeAllChildren()
Here is a simple example for how you can use some of these methodes
class DynamicTreeDemo
===============================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class DynamicTreeDemo extends JFrame {
private int newNodeSuffix = 1;
public DynamicTreeDemo() {
super("Dynamic Tree Demo");
Container contentPane = this.getContentPane();
// Create final instance of this class, see below
final DynamicTree treePanel = new DynamicTree();
JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.addObject("New Node " + newNodeSuffix++);
}
});
JButton removeButton = new JButton("Remove");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.removeCurrentNode();
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
treePanel.clear();
}
});
// Lay everything out.
//............
}
public static void main(String[] args) {
DynamicTreeDemo dtd = new DynamicTreeDemo();
dtd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class DynamicTree
==========================================
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class DynamicTree extends JPanel {
protected DefaultMutableTreeNode rootNode;
protected DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public DynamicTree() {
this.setLayout(new BorderLayout());
rootNode = new DefaultMutableTreeNode("Root Node");
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane, BorderLayout.CENTER);
}
/**
* Remove all nodes except the root node.
*/
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}
/**
* Remove the currently selected node.
*/
public void removeCurrentNode() {
DefaultMutableTreeNode currentNode =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (currentNode != null) {
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)currentNode.getParent();
if (parentNode != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}
// Either there was no selection, or the root was selected.
toolkit.beep();
}
/**
* Add child to the currently selected node.
*/
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (parentNode == null) {
parentNode = rootNode;
}
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
treeModel.insertNodeInto(childNode, parentNode,
parentNode.getChildCount());
// Make sure the user can see the lovely new node.
tree.expandPath(new TreePath(parentNode.getPath()));
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
return childNode;
}
}
Puuh this was much, hopefully you get some helpinformation about a dynamic
JTree
// Andreas
Jan Lacluyse <jan.lacluyse@teleatlas.com> wrote in message
news:38FAE449.E6FF274D@teleatlas.com...
> Hoi,
>
> Can somebody tell me how I can dynamically build a JTree.
> For example a directory structure on a PC.
>
> Jan
>
Technical Details
Message-ID:
<38faf46b@dnews>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38FAE449.E6FF274D@teleatlas.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,9db34bea68a1b60 X-Google-Attributes: gid111d45,public From: "Andreas Andersson" <andreas.andersson@medicinsklogik.se> Subject: Re: Building a JTree - Dynamically Date: 2000/04/17 Message-ID: <38faf46b@dnews> X-Deja-AN: 612189723 References: <38FAE449.E6FF274D@teleatlas.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Trace: 17 Apr 2000 04:24:27 -0800, 193.15.79.66 Organization: MLAB X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.swing