您的位置:首页 > 其它

TreewithCheckbox例子

2009-12-17 23:54 302 查看
Java代码
一.package tree;
2.
三.import java.awt.event.MouseAdapter;
四.import java.awt.event.MouseEvent;
5.
六.import javax.swing.JCheckBox;
七.import javax.swing.JTree;
八.import javax.swing.event.TreeSelectionEvent;
九.import javax.swing.event.TreeSelectionListener;
十.import javax.swing.tree.TreePath;
11.
12.public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener{
13. private CheckTreeSelectionModel selectionModel;
14. private JTree tree = new JTree();
15. int hotspot = new JCheckBox().getPreferredSize().width;
16.
17. public CheckTreeManager(JTree tree){
18. this.tree = tree;
19. selectionModel = new CheckTreeSelectionModel(tree.getModel());
20. tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel));
21. tree.addMouseListener(this);
22. selectionModel.addTreeSelectionListener(this);
23. }
24.
25. public void mouseClicked(MouseEvent me){
26. TreePath path = tree.getPathForLocation(me.getX(), me.getY());
27. if(path==null)
28. return;
29. if(me.getX()>tree.getPathBounds(path).x+hotspot)
30. return;
31.
32. boolean selected = selectionModel.isPathSelected(path, true);
33. selectionModel.removeTreeSelectionListener(this);
34.
35. try{
36. if(selected)
37. selectionModel.removeSelectionPath(path);
38. else
39. selectionModel.addSelectionPath(path);
40. } finally{
41. selectionModel.addTreeSelectionListener(this);
42. tree.treeDidChange();
43. }
44. }
45.
46. public CheckTreeSelectionModel getSelectionModel(){
47. return selectionModel;
48. }
49.
50. public void valueChanged(TreeSelectionEvent e){
51. tree.treeDidChange();
52. }
53.}
package tree;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener{
private CheckTreeSelectionModel selectionModel;
private JTree tree = new JTree();
int hotspot = new JCheckBox().getPreferredSize().width;

public CheckTreeManager(JTree tree){
this.tree = tree;
selectionModel = new CheckTreeSelectionModel(tree.getModel());
tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel));
tree.addMouseListener(this);
selectionModel.addTreeSelectionListener(this);
}

public void mouseClicked(MouseEvent me){
TreePath path = tree.getPathForLocation(me.getX(), me.getY());
if(path==null)
return;
if(me.getX()>tree.getPathBounds(path).x+hotspot)
return;

boolean selected = selectionModel.isPathSelected(path, true);
selectionModel.removeTreeSelectionListener(this);

try{
if(selected)
selectionModel.removeSelectionPath(path);
else
selectionModel.addSelectionPath(path);
} finally{
selectionModel.addTreeSelectionListener(this);
tree.treeDidChange();
}
}

public CheckTreeSelectionModel getSelectionModel(){
return selectionModel;
}

public void valueChanged(TreeSelectionEvent e){
tree.treeDidChange();
}
}

Java代码
一.import java.awt.BorderLayout;
二.import java.awt.Component;
3.
四.import javax.swing.JCheckBox;
五.import javax.swing.JPanel;
六.import javax.swing.JTree;
七.import javax.swing.tree.TreeCellRenderer;
八.import javax.swing.tree.TreePath;
9.
十.public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer{
11. private CheckTreeSelectionModel selectionModel;
12. private TreeCellRenderer delegate;
13. private JCheckBox checkBox = new JCheckBox();
14. // private TristateCheckBox checkBox = new TristateCheckBox();
15.
16. public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
17. this.delegate = delegate;
18. this.selectionModel = selectionModel;
19. setLayout(new BorderLayout());
20. setOpaque(false);
21. checkBox.setOpaque(false);
22. }
23.
24.
25. public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
26. Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
27.
28. TreePath path = tree.getPathForRow(row);
29. if(path!=null){
30. if(selectionModel.isPathSelected(path, true))
31. checkBox.setSelected(Boolean.TRUE);
32. else
33. checkBox.setSelected( Boolean.FALSE);
34. }
35. removeAll();
36. add(checkBox, BorderLayout.WEST);
37. add(renderer, BorderLayout.CENTER);
38. return this;
39. }
40.}
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer{
private CheckTreeSelectionModel selectionModel;
private TreeCellRenderer delegate;
private JCheckBox checkBox = new JCheckBox();
// private TristateCheckBox checkBox = new TristateCheckBox();

public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
this.delegate = delegate;
this.selectionModel = selectionModel;
setLayout(new BorderLayout());
setOpaque(false);
checkBox.setOpaque(false);
}

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

TreePath path = tree.getPathForRow(row);
if(path!=null){
if(selectionModel.isPathSelected(path, true))
checkBox.setSelected(Boolean.TRUE);
else
checkBox.setSelected( Boolean.FALSE);
}
removeAll();
add(checkBox, BorderLayout.WEST);
add(renderer, BorderLayout.CENTER);
return this;
}
}
Java代码
一.package tree;
2.
三.import java.util.ArrayList;
四.import java.util.Stack;
5.
六.import javax.swing.tree.DefaultTreeSelectionModel;
七.import javax.swing.tree.TreeModel;
八.import javax.swing.tree.TreePath;
九.import javax.swing.tree.TreeSelectionModel;
10.
11.public class CheckTreeSelectionModel extends DefaultTreeSelectionModel{
12. private TreeModel model;
13.
14. public CheckTreeSelectionModel(TreeModel model){
15. this.model = model;
16. setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
17. }
18.
19. // tests whether there is any unselected node in the subtree of given path
20. public boolean isPartiallySelected(TreePath path){
21. if(isPathSelected(path, true))
22. return false;
23. TreePath[] selectionPaths = getSelectionPaths();
24. if(selectionPaths==null)
25. return false;
26. for(int j = 零; j27. if(isDescendant(selectionPaths[j], path))
28. return true;
29. }
30. return false;
31. }
32.
33. // tells whether given path is selected.
34. // if dig is true, then a path is assumed to be selected, if
35. // one of its ancestor is selected.
36. public boolean isPathSelected(TreePath path, boolean dig){
37. if(!dig)
38. return super.isPathSelected(path);
39. while(path!=null && !super.isPathSelected(path))
40. path = path.getParentPath();
41. return path!=null;
42. }
43.
44. // is path一 descendant of path二
45. private boolean isDescendant(TreePath path一, TreePath path二){
46. Object obj一[] = path一.getPath();
47. Object obj二[] = path二.getPath();
48. for(int i = 零; i49. if(obj一[i]!=obj二[i])
50. return false;
51. }
52. return true;
53. }
54.
55. public void setSelectionPaths(TreePath[] pPaths){
56. throw new UnsupportedOperationException("not implemented yet!!!");
57. }
58.
59. public void addSelectionPaths(TreePath[] paths){
60. // unselect all descendants of paths[]
61. for(int i = 零; i62. TreePath path = paths[i];
63. TreePath[] selectionPaths = getSelectionPaths();
64. if(selectionPaths==null)
65. break;
66. ArrayList toBeRemoved = new ArrayList();
67. for(int j = 零; j68. if(isDescendant(selectionPaths[j], path))
69. toBeRemoved.add(selectionPaths[j]);
70. }
71. super.removeSelectionPaths((TreePath[])toBeRemoved.toArray(new TreePath[零]));
72. }
73.
74. // if all siblings are selected then unselect them and select parent recursively
75. // otherwize just select that path.
76. for(int i = 零; i77. TreePath path = paths[i];
78. TreePath temp = null;
79. while(areSiblingsSelected(path)){
80. temp = path;
81. if(path.getParentPath()==null)
82. break;
83. path = path.getParentPath();
84. }
85. if(temp!=null){
86. if(temp.getParentPath()!=null)
87. addSelectionPath(temp.getParentPath());
88. else{
89. if(!isSelectionEmpty())
90. removeSelectionPaths(getSelectionPaths());
91. super.addSelectionPaths(new TreePath[]{temp});
92. }
93. }else
94. super.addSelectionPaths(new TreePath[]{ path});
95. }
96. }
97.
98. // tells whether all siblings of given path are selected.
99. private boolean areSiblingsSelected(TreePath path){
100. TreePath parent = path.getParentPath();
101. if(parent==null)
102. return true;
103. Object node = path.getLastPathComponent();
104. Object parentNode = parent.getLastPathComponent();
105.
106. int childCount = model.getChildCount(parentNode);
107. for(int i = 零; i108. Object childNode = model.getChild(parentNode, i);
109. if(childNode==node)
110. continue;
111. if(!isPathSelected(parent.pathByAddingChild(childNode)))
112. return false;
113. }
114. return true;
115. }
116.
117. public void removeSelectionPaths(TreePath[] paths){
118. for(int i = 零; i119. TreePath path = paths[i];
120. if(path.getPathCount()==一)
121. super.removeSelectionPaths(new TreePath[]{ path});
122. else
123. toggleRemoveSelection(path);
124. }
125. }
126.
127. // if any ancestor node of given path is selected then unselect it
128. // and selection all its descendants except given path and descendants.
129. // otherwise just unselect the given path
130. private void toggleRemoveSelection(TreePath path){
131. Stack stack = new Stack();
132. TreePath parent = path.getParentPath();
133. while(parent!=null && !isPathSelected(parent)){
134. stack.push(parent);
135. parent = parent.getParentPath();
136. }
137. if(parent!=null)
138. stack.push(parent);
139. else{
140. super.removeSelectionPaths(new TreePath[]{path});
141. return;
142. }
143.
144. while(!stack.isEmpty()){
145. TreePath temp = (TreePath)stack.pop();
146. TreePath peekPath = stack.isEmpty() ? path : (TreePath)stack.peek();
147. Object node = temp.getLastPathComponent();
148. Object peekNode = peekPath.getLastPathComponent();
149. int childCount = model.getChildCount(node);
150. for(int i = 零; i151. Object childNode = model.getChild(node, i);
152. if(childNode!=peekNode)
153. super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});
154. }
155. }
156. super.removeSelectionPaths(new TreePath[]{parent});
157. }
158.}
package tree;

import java.util.ArrayList;
import java.util.Stack;

import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public class CheckTreeSelectionModel extends DefaultTreeSelectionModel{
private TreeModel model;

public CheckTreeSelectionModel(TreeModel model){
this.model = model;
setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
}

// tests whether there is any unselected node in the subtree of given path
public boolean isPartiallySelected(TreePath path){
if(isPathSelected(path, true))
return false;
TreePath[] selectionPaths = getSelectionPaths();
if(selectionPaths==null)
return false;
for(int j = 零; j if(isDescendant(selectionPaths[j], path))
return true;
}
return false;
}

// tells whether given path is selected.
// if dig is true, then a path is assumed to be selected, if
// one of its ancestor is selected.
public boolean isPathSelected(TreePath path, boolean dig){
if(!dig)
return super.isPathSelected(path);
while(path!=null && !super.isPathSelected(path))
path = path.getParentPath();
return path!=null;
}

// is path一 descendant of path二
private boolean isDescendant(TreePath path一, TreePath path二){
Object obj一[] = path一.getPath();
Object obj二[] = path二.getPath();
for(int i = 零; i if(obj一[i]!=obj二[i])
return false;
}
return true;
}

public void setSelectionPaths(TreePath[] pPaths){
throw new UnsupportedOperationException("not implemented yet!!!");
}

public void addSelectionPaths(TreePath[] paths){
// unselect all descendants of paths[]
for(int i = 零; i TreePath path = paths[i];
TreePath[] selectionPaths = getSelectionPaths();
if(selectionPaths==null)
break;
ArrayList toBeRemoved = new ArrayList();
for(int j = 零; j if(isDescendant(selectionPaths[j], path))
toBeRemoved.add(selectionPaths[j]);
}
super.removeSelectionPaths((TreePath[])toBeRemoved.toArray(new TreePath[零]));
}

// if all siblings are selected then unselect them and select parent recursively
// otherwize just select that path.
for(int i = 零; i TreePath path = paths[i];
TreePath temp = null;
while(areSiblingsSelected(path)){
temp = path;
if(path.getParentPath()==null)
break;
path = path.getParentPath();
}
if(temp!=null){
if(temp.getParentPath()!=null)
addSelectionPath(temp.getParentPath());
else{
if(!isSelectionEmpty())
removeSelectionPaths(getSelectionPaths());
super.addSelectionPaths(new TreePath[]{temp});
}
}else
super.addSelectionPaths(new TreePath[]{ path});
}
}

// tells whether all siblings of given path are selected.
private boolean areSiblingsSelected(TreePath path){
TreePath parent = path.getParentPath();
if(parent==null)
return true;
Object node = path.getLastPathComponent();
Object parentNode = parent.getLastPathComponent();

int childCount = model.getChildCount(parentNode);
for(int i = 零; i Object childNode = model.getChild(parentNode, i);
if(childNode==node)
continue;
if(!isPathSelected(parent.pathByAddingChild(childNode)))
return false;
}
return true;
}

public void removeSelectionPaths(TreePath[] paths){
for(int i = 零; i TreePath path = paths[i];
if(path.getPathCount()==一)
super.removeSelectionPaths(new TreePath[]{ path});
else
toggleRemoveSelection(path);
}
}

// if any ancestor node of given path is selected then unselect it
// and selection all its descendants except given path and descendants.
// otherwise just unselect the given path
private void toggleRemoveSelection(TreePath path){
Stack stack = new Stack();
TreePath parent = path.getParentPath();
while(parent!=null && !isPathSelected(parent)){
stack.push(parent);
parent = parent.getParentPath();
}
if(parent!=null)
stack.push(parent);
else{
super.removeSelectionPaths(new TreePath[]{path});
return;
}

while(!stack.isEmpty()){
TreePath temp = (TreePath)stack.pop();
TreePath peekPath = stack.isEmpty() ? path : (TreePath)stack.peek();
Object node = temp.getLastPathComponent();
Object peekNode = peekPath.getLastPathComponent();
int childCount = model.getChildCount(node);
for(int i = 零; i Object childNode = model.getChild(node, i);
if(childNode!=peekNode)
super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});
}
}
super.removeSelectionPaths(new TreePath[]{parent});
}
}

运用:

CheckTreeManager checkTreeManager = new CheckTreeManager(yourTree);

// to get the paths that were checked
TreePath checkedPaths[] = checkTreeManager.getSelectionModel().getSelectionPaths();

本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception

1150 - invalid username password

1151 - 存储过程数组错误

1152 - Syntax error on token(s)

1153 - EXP-00056:遇到oracle错误12154

1154 - Agent Service Failed

1155 - ORA-00988:missing or invalid password(s)

1156 - 创建的过程带有编译错误

1157 - PLS-00302

1158 - java.lang.Exception: Invalid EJB component

1159 - 必须重新启动计算机后才能安装visual Studio.NET系统必备

1160 - System.ExecutionEngineException

1161 - 如何捕捉网络响应超时错误

1162 - 因为他是Web方法

1163 - A circular reference was detected while serializing an object of type

1164 - 如何调用wbeServer中的方法并直接取得回传的XML

1165 - System.IO.FileNotFoundException

1166 - 创建ASP.NET WEB 服务时出错

1167 - 如何使用override,virtual

1168 - .net异常的处理

1169 - unable to connect tothe remote server
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: