Christophe said:
Knute Johnson wrote:
Your idea is about just storing the TreeModel is correct, but what do you
mean by, "it doesn't work" ?
Serializing the model, and using it to reconstruct your JTree should work.
How are you doing this exactly?
I've attached the test code below. The objects that are added are
Serializable. I just attempt to write the TreeModel to a file with
ObjectOutputStream and then read it back when the program starts. Start
it at a command line to see the debugging messages.
Thanks again,
knute...
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeTest extends JFrame {
DefaultMutableTreeNode rootNode;
DefaultTreeModel treeModel;
JTree tree;
public TreeTest() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("treeModel.dat"));
oos.writeObject(treeModel);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
});
Container CP = getContentPane();
CP.setLayout(new FlowLayout());
TreeModelListener tml = new TreeModelListener() {
public void treeNodesChanged(TreeModelEvent tme) {
System.out.println("treeNodesChaged");
}
public void treeNodesInserted(TreeModelEvent tme) {
System.out.println("treeNodesInserted");
}
public void treeNodesRemoved(TreeModelEvent tme) {
System.out.println("treeNodesRemoved");
}
public void treeStructureChanged(TreeModelEvent tme) {
System.out.println("treeStructureChanged");
}
};
try {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("treeModel.dat"));
treeModel = (DefaultTreeModel)ois.readObject();
rootNode = (DefaultMutableTreeNode)treeModel.getRoot();
ois.close();
} catch (Exception e) {
e.printStackTrace();
rootNode = new DefaultMutableTreeNode("Client Project List");
treeModel = new DefaultTreeModel(rootNode);
}
treeModel.addTreeModelListener(tml);
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
CP.add(tree);
JButton addClient = new JButton("Add Client");
addClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String str = JOptionPane.showInputDialog(tree,
"Enter Client Name");
if (str != null) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(new Client(str));
treeModel.insertNodeInto(childNode,rootNode,
rootNode.getChildCount());
tree.scrollPathToVisible(new
TreePath(childNode.getPath()));
}
}
});
CP.add(addClient);
JButton addProject = new JButton("Add Project");
addProject.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
DefaultMutableTreeNode selectedNode =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (selectedNode != null) {
if (selectedNode.getUserObject() instanceof Client) {
String str = JOptionPane.showInputDialog(tree,
"Enter Project Name");
if (str != null) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(new Project(str));
treeModel.insertNodeInto(childNode,selectedNode,
selectedNode.getChildCount());
tree.scrollPathToVisible(
new TreePath(childNode.getPath()));
}
}
}
}
});
CP.add(addProject);
JButton del = new JButton("Delete");
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
DefaultMutableTreeNode selectedNode =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (selectedNode != null &&
!selectedNode.equals(rootNode))
treeModel.removeNodeFromParent(selectedNode);
}
});
CP.add(del);
setSize(640,480);
setVisible(true);
}
public class Client implements Serializable {
String clientName;
public Client(String name) {
this.clientName = name;
}
public String toString() {
return clientName;
}
}
public class Project implements Serializable {
String projectName;
public Project(String name) {
this.projectName = name;
}
public String toString() {
return projectName;
}
}
public static void main(String[] args) {
new TreeTest();
}
}