例子中不能保存状态,原来例子用的是model.reload方法,我必须用自己写的方法从数据库中提取数据,我实在看不懂了,大家帮我看看吧!分不多了,有了再加!(内容太长,转下页)
package pk.tree;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;public class TreeExpandedRestorer {
/**
* Selections are stored with path information. If you plan to insert different nodes, you should use this option.
*/
public static final SelectionType SELECTION_BY_PATH = new SelectionType("SelectionByPath");
/**
* Selections are stored with row information. If you decide to delete nodes, you should select this option.
*/
public static final SelectionType SELECTION_BY_ROW = new SelectionType("SelectionByRow");
private List expandedPaths;
private int[] selectionRows;
private JTree tree;
/**
* Saves the expanded state and selection of the tree.
* @param tree the tree for which selection and expanstion state are saved
* @param selectionType the type of selection, either {@link #SELECTION_BY_ROW} or {@link #SELECTION_BY_PATH}
*/
public TreeExpandedRestorer(JTree tree, SelectionType selectionType) {
this(tree, false);
}
/**
* Saves the expanded state and optionally discards the selection of the tree.
* @param tree the tree for which selection and expanstion state are saved
* @param clearSelection whether to discard selection
*/
public TreeExpandedRestorer(JTree tree, boolean clearSelection) {
this.tree = tree;
save();
if (clearSelection) clearSelection();
}
/**
* Saves selected node and all expanded paths.
*/
public void save() {
saveSelection();
saveExpandedState();
}
/**
* Use this method if you want to transfer a node with a tree, and you want to have the selection transfered too.
* @param target the node being transfered
* @param old the source tree path where the node was or is attached to
* @param nw the destination tree path where the node will be or is attached to
*/
public void update(TreeNode target, TreePath old, TreePath nw) {
TreePath[] tp = (TreePath[]) expandedPaths.toArray(new TreePath[0]);
for (int i = 0; i < tp.length; i++) {
Object[] objs = tp[i].getPath();
for (int j = 0; j < objs.length; j++) {
if (objs[j] == target) {
Object[] newPath = new Object[objs.length - old.getPathCount() + nw.getPathCount()];
System.arraycopy(nw.getPath(), 0, newPath, 0, nw.getPathCount());
System.arraycopy(objs, j, newPath, nw.getPathCount(), objs.length - j);
tp[i] = new TreePath(newPath);
}
}
}
}
public void remove(TreePath tp) {
for (Iterator iterator = expandedPaths.iterator(); iterator.hasNext();) {
TreePath path = (TreePath) iterator.next();
if (tp.isDescendant(path)) iterator.remove();
}
}
/**
* Expands all paths last saved and restores selected node.
* @param selectionType the type of selection, either {@link #SELECTION_BY_ROW} or {@link #SELECTION_BY_PATH}
*/
public void restore(SelectionType selectionType) {
restoreExpandedState();
restoreSelections(selectionType);
}
public void clearSelection() {
selectionRows = new int[0];
}
package pk.tree;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;public class TreeExpandedRestorer {
/**
* Selections are stored with path information. If you plan to insert different nodes, you should use this option.
*/
public static final SelectionType SELECTION_BY_PATH = new SelectionType("SelectionByPath");
/**
* Selections are stored with row information. If you decide to delete nodes, you should select this option.
*/
public static final SelectionType SELECTION_BY_ROW = new SelectionType("SelectionByRow");
private List expandedPaths;
private int[] selectionRows;
private JTree tree;
/**
* Saves the expanded state and selection of the tree.
* @param tree the tree for which selection and expanstion state are saved
* @param selectionType the type of selection, either {@link #SELECTION_BY_ROW} or {@link #SELECTION_BY_PATH}
*/
public TreeExpandedRestorer(JTree tree, SelectionType selectionType) {
this(tree, false);
}
/**
* Saves the expanded state and optionally discards the selection of the tree.
* @param tree the tree for which selection and expanstion state are saved
* @param clearSelection whether to discard selection
*/
public TreeExpandedRestorer(JTree tree, boolean clearSelection) {
this.tree = tree;
save();
if (clearSelection) clearSelection();
}
/**
* Saves selected node and all expanded paths.
*/
public void save() {
saveSelection();
saveExpandedState();
}
/**
* Use this method if you want to transfer a node with a tree, and you want to have the selection transfered too.
* @param target the node being transfered
* @param old the source tree path where the node was or is attached to
* @param nw the destination tree path where the node will be or is attached to
*/
public void update(TreeNode target, TreePath old, TreePath nw) {
TreePath[] tp = (TreePath[]) expandedPaths.toArray(new TreePath[0]);
for (int i = 0; i < tp.length; i++) {
Object[] objs = tp[i].getPath();
for (int j = 0; j < objs.length; j++) {
if (objs[j] == target) {
Object[] newPath = new Object[objs.length - old.getPathCount() + nw.getPathCount()];
System.arraycopy(nw.getPath(), 0, newPath, 0, nw.getPathCount());
System.arraycopy(objs, j, newPath, nw.getPathCount(), objs.length - j);
tp[i] = new TreePath(newPath);
}
}
}
}
public void remove(TreePath tp) {
for (Iterator iterator = expandedPaths.iterator(); iterator.hasNext();) {
TreePath path = (TreePath) iterator.next();
if (tp.isDescendant(path)) iterator.remove();
}
}
/**
* Expands all paths last saved and restores selected node.
* @param selectionType the type of selection, either {@link #SELECTION_BY_ROW} or {@link #SELECTION_BY_PATH}
*/
public void restore(SelectionType selectionType) {
restoreExpandedState();
restoreSelections(selectionType);
}
public void clearSelection() {
selectionRows = new int[0];
}
public void setSelection(TreePath path) {
selectionRows = new int[1];
selectionRows[0] = tree.getRowForPath(path);
}
public void addSelection(TreePath path) {
int[] newRows = new int[selectionRows.length + 1];
for (int i = 0; i < selectionRows.length; i++) {
newRows[i] = selectionRows[i];
}
newRows[selectionRows.length] = tree.getRowForPath(path);
selectionRows = newRows;
}
/**
*
* @param selectionType the type of selection, either {@link #SELECTION_BY_ROW} or {@link #SELECTION_BY_PATH}
*/
private void restoreSelections(SelectionType selectionType) {
if (selectionType == SELECTION_BY_ROW) {
if (selectionRows == null) return;
for (int i = 0; i < selectionRows.length; i++) {
int selectionRow = selectionRows[i];
selectionRows[i] = Math.min(selectionRow, tree.getRowCount() - 1);
}
tree.setSelectionRows(selectionRows);
} else {
tree.setSelectionPaths((TreePath[])expandedPaths.toArray(new TreePath[0]));
}
}
private void restoreExpandedState() {
for (Iterator iterator = expandedPaths.iterator(); iterator.hasNext();) {
TreePath path = (TreePath) iterator.next();
tree.expandPath(path);
}
}
private void saveExpandedState() {
expandedPaths = new ArrayList();
TreePath rootPath = new TreePath(((TreeNode) tree.getModel().getRoot()));
Enumeration enum = tree.getExpandedDescendants(rootPath);
while (enum != null && enum.hasMoreElements()) {
expandedPaths.add(enum.nextElement());
}
}
private void saveSelection() {
TreePath[] tp = tree.getSelectionPaths();
if (tp != null) {
selectionRows = new int[tp.length];
for (int i = 0; i < tp.length; i++) {
TreePath treePath = tp[i];
selectionRows[i] = tree.getRowForPath(treePath);
}
} else {
selectionRows = null;
}
}
private static class SelectionType {
private String type;
private SelectionType(String type) {
this.type = type;
}
}
private static DefaultTreeModel getModel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
for (int i=0;i<10;i++)
{
DefaultMutableTreeNode second = new DefaultMutableTreeNode("ssss"+Integer.toString(i));
for(int j=0;j<3;j++)
{
DefaultMutableTreeNode third = new DefaultMutableTreeNode("ttttt"+Integer.toString(i));
second.add(third);
}
root.add(second);
}
DefaultTreeModel dtm = new DefaultTreeModel(root);
return dtm;
}
public static void main(String[] args) { final javax.swing.JTree tree11 = new javax.swing.JTree(getModel());
//final DefaultTreeModel model = (DefaultTreeModel)tree11.getModel();
javax.swing.JButton reload = new javax.swing.JButton("Reload");
reload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tree11.setModel(getModel());
System.out.println("reloaded...");
}
});
final TreeExpandedRestorer ter = new TreeExpandedRestorer(tree11, SELECTION_BY_ROW);
javax.swing.JButton save = new javax.swing.JButton("Save");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected path before ter.save is: " + tree11.getSelectionPath());
ter.save();
tree11.setModel(getModel());
System.out.println("saved...");
}
});
javax.swing.JButton restore = new javax.swing.JButton("Restore");
restore.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected path before ter.restore is: " + tree11.getSelectionPath());
ter.restore(SELECTION_BY_ROW);
System.out.println("restored...");
}
});
javax.swing.JButton print = new javax.swing.JButton("Print");
print.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Selected path is: " + tree11.getSelectionPath());
}
});
javax.swing.JPanel p = new javax.swing.JPanel();
p.add(save);
p.add(reload);
p.add(restore);
p.add(print);
javax.swing.JFrame f = new javax.swing.JFrame();
f.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new java.awt.BorderLayout());
f.getContentPane().add(p, java.awt.BorderLayout.NORTH);
f.getContentPane().add(new javax.swing.JScrollPane(tree11), java.awt.BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
}
}
btb368() ( )
就是没有错误,可是我就改了一句就不能实现我的保存了,只能保存选中,不能保存展开状态!你只要拈贴下来就能运行,也能明白我的需求了!
expandedPaths = new ArrayList();
TreePath rootPath = new TreePath(((TreeNode) tree.getModel().getRoot()));
Enumeration enum = tree.getExpandedDescendants(rootPath);
while (enum != null && enum.hasMoreElements()) {
expandedPaths.add(enum.nextElement());
}
}
reload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
tree11.setModel(getModel());
System.out.println("reloaded...");
}
});这句是楼主自己改的吧?=====>tree11.setModel(getModel());
JButton("Save")中有setModel了,赫赫!
public boolean equals(Object o) {
if(o!=null)
return o.toString().trim().equals(this.toString().trim());
return false;
}
然后所有的TreeNode用继承的DefaultMutableTreeNode来构造时间比较急,我没试,你可以自己实验下。我有事先走了。有问题以后我会来看
我点击左侧导航树,修改一些右侧对应的该节点信息,比如性别,姓名等,回存到数据库中,不从数据库中刷新树如何实现男的节点变为兰色,女的为红色;
节点还有其他属性影响颜色
我点击左侧导航树,修改一些右侧对应的该节点信息,比如性别,姓名等,回存到数据库中,不从数据库中刷新树如何实现男的节点变为兰色,女的为红色;
节点还有其他属性影响颜色这些细节的东西可以使用TreeCellRenderer来实现,建议看看Table和Tree的CellEditor和CellRenderer,呵呵,这些都是蛮基础的东西