JTree Zustand speichern
Aus Byte-Welt Wiki
Version vom 30. März 2009, 18:18 Uhr von AndreUhres (Diskussion | Beiträge)
Dieses Beispiel zeigt, wie wir den Zustand von einem JTree speichern können, so daß er nach einem Programmneustart wieder geladen werden kann.
<code=java>/*
* TreeStatusDemo.java * * This example shows you how to store the tree status with XMLEncoder * so that it can be reloaded after program restart. * Maybe it can help you. * * Btw, we can use XMLEncoder not only to store the tree status, but for storing any * object we like. To do so, it's enough that the objects respect the "Bean" format, * which is extremely simple: * * a public class where we add a default constructor (without parameters) * and the setXXX and getXXX (or isXXX) methods for the properties. * That's already all for it to work! */
import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.*; import java.util.*; import java.util.List; import javax.swing.*; import javax.swing.tree.*;
public class TreeStatusDemo extends JFrame {
private JToolBar toolbar; private JButton btAdd; private JTree tree; private TreeModel model; private List<TreeNode[]> expanded; private int countNew;
public TreeStatusDemo() { super("Tree Status Demo"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); toolbar = new JToolBar(); btAdd = new JButton("Add Node"); tree = new JTree(); toolbar.add(btAdd); getContentPane().add(toolbar, BorderLayout.NORTH); tree.setEditable(true); getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); try { deSerialize(); restoreStatus(); } catch (Exception e) { } model = tree.getModel(); btAdd.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) { addNode(); } }); addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) { saveStatus(); serialize(); System.exit(0); } }); }
private void saveStatus() { //save the expanded Nodes: expanded = new ArrayList<TreeNode[]>(); collectStatusInfo(model.getRoot()); }
private void restoreStatus() { //expand the saved Nodes: for (int i = expanded.size() - 1; i > -1; i--) { TreeNode[] ar = expanded.get(i); tree.expandPath(new TreePath(ar)); } }
private void collectStatusInfo(final Object o) { int cc = model.getChildCount(o); for (int i = 0; i < cc; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(o, i); TreeNode[] ar = child.getPath(); if (!model.isLeaf(child) && tree.isExpanded(new TreePath(ar))) { expanded.add(ar); collectStatusInfo(child); } } }
private void addNode() { TreePath selectedPath = tree.getSelectionPath(); if (selectedPath == null) { return; } DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectedPath.getLastPathComponent(); DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("new node " + countNew++); ((DefaultTreeModel) tree.getModel()).insertNodeInto(newChild, selectedNode, selectedNode.getChildCount()); TreePath newPath = selectedPath.pathByAddingChild(newChild); tree.setSelectionPath(newPath); tree.startEditingAtPath(newPath); }
public void serialize() { try { XMLEncoder o = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("tree.xml"))); o.writeObject(tree.getModel()); o.writeObject(expanded); o.close(); } catch (IOException e) { e.printStackTrace(); } }
@SuppressWarnings("unchecked") private void deSerialize() { try { XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream("tree.xml"))); tree.setModel((TreeModel) d.readObject()); expanded = (List<TreeNode[]>) d.readObject(); d.close(); } catch (FileNotFoundException ex) { } }
public static void main(final String args[]) { SwingUtilities.invokeLater(new Runnable() {
public void run() { new TreeStatusDemo().setVisible(true); } }); }
}</code=java>