当前位置: 技术问答>java相关
如何利用JDOM,把JTree中所有节点写入到一个XML文件中?最好有现成的例子。谢谢了。
来源: 互联网 发布时间:2015-11-13
本文导语: rt......... 能给出链接地址最好,或者具体的例子。 本人急用。。。。。 谢谢。。。。。。 | import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; import java.beans.*; import java.io.*; impo...
rt.........
能给出链接地址最好,或者具体的例子。
本人急用。。。。。
谢谢。。。。。。
能给出链接地址最好,或者具体的例子。
本人急用。。。。。
谢谢。。。。。。
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.beans.*;
import java.io.*;
import javax.swing.event.*;
import java.beans.ExceptionListener;
public class DynamicTreeDemo extends JPanel implements ExceptionListener {
private int newNodeSuffix = 1;
String xmlFname = "test.xml";
DynamicTree treePanel = new DynamicTree();
public DynamicTreeDemo(JFrame frame) {
populateTree(treePanel);
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();
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream( xmlFname ) ) );
xe.setExceptionListener(DynamicTreeDemo.this);
xe.writeObject( treePanel.rootNode );
xe.flush();
xe.close();
}
catch(Exception ex)
{ex.printStackTrace();}
}
});
JButton loadButton = new JButton("load");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
treePanel.clear();
XMLDecoder xe = new XMLDecoder(new FileInputStream(xmlFname));
xe.setExceptionListener(DynamicTreeDemo.this);
treePanel.rootNode = (DefaultMutableTreeNode)xe.readObject();
xe.close();
treePanel.reload();
}
catch(Exception ex)
{ex.printStackTrace();}
}
});
setLayout(new BorderLayout());
treePanel.setPreferredSize(new Dimension(300, 150));
add(treePanel, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.add(addButton);
panel.add(removeButton);
panel.add(clearButton);
panel.add(saveButton);
panel.add(loadButton);
add(panel, BorderLayout.EAST);
}
public void populateTree(DynamicTree treePanel) {
String p1Name = new String("Parent 1");
String p2Name = new String("Parent 2");
String c1Name = new String("Child 1");
String c2Name = new String("Child 2");
DefaultMutableTreeNode p1, p2;
p1 = treePanel.addObject(null, p1Name);
p2 = treePanel.addObject(null, p2Name);
treePanel.addObject(p1, c1Name);
treePanel.addObject(p1, c2Name);
treePanel.addObject(p2, c1Name);
treePanel.addObject(p2, c2Name);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Save tree to xml and load from that xml file:masterz contributes the save/load part");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(new DynamicTreeDemo(frame));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
public void exceptionThrown(Exception e) {
e.printStackTrace();
}
}
class DynamicTree extends JPanel {
public DefaultMutableTreeNode rootNode;
public DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public DynamicTree() {
rootNode = new DefaultMutableTreeNode("Root Node");
reload();
}
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}
public void reload()
{
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
this.removeAll();
JScrollPane scrollPane = new JScrollPane(tree);
setLayout(new GridLayout(1,0));
add(scrollPane);
invalidate();
repaint();
validate();
}
/** Remove the currently selected node. */
public void removeCurrentNode() {
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
(currentSelection.getLastPathComponent());
MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
if (parent != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}
}
/** Add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child) {
return addObject(parent, child, false);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
if (parent == null) {
parent = rootNode;
}
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.beans.*;
import java.io.*;
import javax.swing.event.*;
import java.beans.ExceptionListener;
public class DynamicTreeDemo extends JPanel implements ExceptionListener {
private int newNodeSuffix = 1;
String xmlFname = "test.xml";
DynamicTree treePanel = new DynamicTree();
public DynamicTreeDemo(JFrame frame) {
populateTree(treePanel);
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();
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream( xmlFname ) ) );
xe.setExceptionListener(DynamicTreeDemo.this);
xe.writeObject( treePanel.rootNode );
xe.flush();
xe.close();
}
catch(Exception ex)
{ex.printStackTrace();}
}
});
JButton loadButton = new JButton("load");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
treePanel.clear();
XMLDecoder xe = new XMLDecoder(new FileInputStream(xmlFname));
xe.setExceptionListener(DynamicTreeDemo.this);
treePanel.rootNode = (DefaultMutableTreeNode)xe.readObject();
xe.close();
treePanel.reload();
}
catch(Exception ex)
{ex.printStackTrace();}
}
});
setLayout(new BorderLayout());
treePanel.setPreferredSize(new Dimension(300, 150));
add(treePanel, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,1));
panel.add(addButton);
panel.add(removeButton);
panel.add(clearButton);
panel.add(saveButton);
panel.add(loadButton);
add(panel, BorderLayout.EAST);
}
public void populateTree(DynamicTree treePanel) {
String p1Name = new String("Parent 1");
String p2Name = new String("Parent 2");
String c1Name = new String("Child 1");
String c2Name = new String("Child 2");
DefaultMutableTreeNode p1, p2;
p1 = treePanel.addObject(null, p1Name);
p2 = treePanel.addObject(null, p2Name);
treePanel.addObject(p1, c1Name);
treePanel.addObject(p1, c2Name);
treePanel.addObject(p2, c1Name);
treePanel.addObject(p2, c2Name);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Save tree to xml and load from that xml file:masterz contributes the save/load part");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(new DynamicTreeDemo(frame));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
public void exceptionThrown(Exception e) {
e.printStackTrace();
}
}
class DynamicTree extends JPanel {
public DefaultMutableTreeNode rootNode;
public DefaultTreeModel treeModel;
protected JTree tree;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public DynamicTree() {
rootNode = new DefaultMutableTreeNode("Root Node");
reload();
}
public void clear() {
rootNode.removeAllChildren();
treeModel.reload();
}
public void reload()
{
treeModel = new DefaultTreeModel(rootNode);
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
this.removeAll();
JScrollPane scrollPane = new JScrollPane(tree);
setLayout(new GridLayout(1,0));
add(scrollPane);
invalidate();
repaint();
validate();
}
/** Remove the currently selected node. */
public void removeCurrentNode() {
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
(currentSelection.getLastPathComponent());
MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
if (parent != null) {
treeModel.removeNodeFromParent(currentNode);
return;
}
}
}
/** Add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child) {
return addObject(parent, child, false);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
if (parent == null) {
parent = rootNode;
}
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
}
|
关于XML和JTree的映射
我在SUN的Java Web Services Tutorial里看到过一个例子
不过是从XML-->JTree的
反向应该不是很困难吧
现成的例子倒是没有,呵呵
我在SUN的Java Web Services Tutorial里看到过一个例子
不过是从XML-->JTree的
反向应该不是很困难吧
现成的例子倒是没有,呵呵