问题是这样的我用
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root", true);
fileTree = new JTree(root);
构造了一个fileTree
并用两个类作为fileTree的节点: public class MyTreeNodeFile{// extends DefaultMutableTreeNode{
public String nodeName;
public String nodeContent;
public int nodeSize;
public String createDate = "";
public String changeDate = "";
private static final long serialVersionUID = 1L; MyTreeNodeFile(int size, String name){
nodeContent = "";
nodeName = name;
nodeSize = size;
createDate = String.format("%tr", new Date());
}
public void setString(String content){
nodeContent = content;
}
public String toString() {
return nodeName;
}
public int getNodeSize(){
nodeSize = nodeContent.length();
return nodeSize;
}
public void setChangeDate(String date){
changeDate = date;
}
}
class MyTreeNodeFolder{// extends DefaultMutableTreeNode{
public String nodeName;
public String createDate = "";
private static final long serialVersionUID = 1L;
MyTreeNodeFolder(String name){
nodeName = name;
createDate = String.format("%tr", new Date());
}
public String toString() {
return nodeName;
}
}
在程序中通过两个不同的按钮插入这两种节点:
public class MyAddFileButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)fileTree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
String newName = JOptionPane.showInputDialog("请输入新文件名称", "Newfile");
if(newName != null){
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new MyTreeNodeFile(0, newName));
newNode.setAllowsChildren(false);
selectedNode.add(newNode);
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
fileTree.scrollPathToVisible(path);
fileTree.updateUI();
}
}
}
public class MyAddFolderButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)fileTree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
String newName = JOptionPane.showInputDialog("请输入新文件夹名称", "NewFolder");
if(newName != null){
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new MyTreeNodeFolder(newName));
newNode.setAllowsChildren(true);
selectedNode.add(newNode);
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
fileTree.scrollPathToVisible(path);
fileTree.updateUI();
}
}
}
当fileTree构造完成后我希望序列化存储这个JTree用来保存(包括所有节点里面的数据),以便下次打开时还原现场
于是我这样操作:
try {
model = (DefaultTreeModel)fileTree.getModel();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("t.dat")); os.writeObject(model);
os.close();
}catch (IOException ex) {
ex.printStackTrace();
}但是系统说我没有时间Srializble接口请问我该怎么做?????谢谢
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root", true);
fileTree = new JTree(root);
构造了一个fileTree
并用两个类作为fileTree的节点: public class MyTreeNodeFile{// extends DefaultMutableTreeNode{
public String nodeName;
public String nodeContent;
public int nodeSize;
public String createDate = "";
public String changeDate = "";
private static final long serialVersionUID = 1L; MyTreeNodeFile(int size, String name){
nodeContent = "";
nodeName = name;
nodeSize = size;
createDate = String.format("%tr", new Date());
}
public void setString(String content){
nodeContent = content;
}
public String toString() {
return nodeName;
}
public int getNodeSize(){
nodeSize = nodeContent.length();
return nodeSize;
}
public void setChangeDate(String date){
changeDate = date;
}
}
class MyTreeNodeFolder{// extends DefaultMutableTreeNode{
public String nodeName;
public String createDate = "";
private static final long serialVersionUID = 1L;
MyTreeNodeFolder(String name){
nodeName = name;
createDate = String.format("%tr", new Date());
}
public String toString() {
return nodeName;
}
}
在程序中通过两个不同的按钮插入这两种节点:
public class MyAddFileButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)fileTree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
String newName = JOptionPane.showInputDialog("请输入新文件名称", "Newfile");
if(newName != null){
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new MyTreeNodeFile(0, newName));
newNode.setAllowsChildren(false);
selectedNode.add(newNode);
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
fileTree.scrollPathToVisible(path);
fileTree.updateUI();
}
}
}
public class MyAddFolderButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)fileTree.getLastSelectedPathComponent();
if (selectedNode == null) {
return;
}
String newName = JOptionPane.showInputDialog("请输入新文件夹名称", "NewFolder");
if(newName != null){
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new MyTreeNodeFolder(newName));
newNode.setAllowsChildren(true);
selectedNode.add(newNode);
TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
fileTree.scrollPathToVisible(path);
fileTree.updateUI();
}
}
}
当fileTree构造完成后我希望序列化存储这个JTree用来保存(包括所有节点里面的数据),以便下次打开时还原现场
于是我这样操作:
try {
model = (DefaultTreeModel)fileTree.getModel();
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("t.dat")); os.writeObject(model);
os.close();
}catch (IOException ex) {
ex.printStackTrace();
}但是系统说我没有时间Srializble接口请问我该怎么做?????谢谢
class A implements Srializble {
...
}
这样A的实例才能序列化