我有一個DeafultTreeModel樹,它有三個級別的節點。我希望能夠讓根節點及其直接子節點不可選。而第三級兒童節點應該是可選擇的。有人可以幫助我這個。我是Swing的新手,希望有人能幫助我,因爲我無法在任何地方找到答案。用小孩創建不可選擇的DefaultTreeModel節點
2
A
回答
8
正如@MadProgrammer所解釋的那樣,您需要提供您自己的選擇模型。基本策略是通過包裝默認選擇模型並將所有方法調用委託給它來實現選擇模型,但是過濾不希望被選擇的TreePath。
這是一個小例子。它採用的方法canPathBeAdded
過濾路徑選擇:
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.RowMapper;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class TestTreeSelection {
private class MyTreeSelectionModel implements TreeSelectionModel {
TreeSelectionModel selectionModel = new DefaultTreeSelectionModel();
private boolean canPathBeAdded(TreePath treePath) {
return treePath.getPathCount() > 2;
}
private TreePath[] getFilteredPaths(TreePath[] paths) {
List<TreePath> returnedPaths = new ArrayList<TreePath>(paths.length);
for (TreePath treePath : paths) {
if (canPathBeAdded(treePath)) {
returnedPaths.add(treePath);
}
}
return returnedPaths.toArray(new TreePath[returnedPaths.size()]);
}
@Override
public void setSelectionMode(int mode) {
selectionModel.setSelectionMode(mode);
}
@Override
public int getSelectionMode() {
return selectionModel.getSelectionMode();
}
@Override
public void setSelectionPath(TreePath path) {
if (canPathBeAdded(path)) {
selectionModel.setSelectionPath(path);
}
}
@Override
public void setSelectionPaths(TreePath[] paths) {
paths = getFilteredPaths(paths);
selectionModel.setSelectionPaths(paths);
}
@Override
public void addSelectionPath(TreePath path) {
if (canPathBeAdded(path)) {
selectionModel.addSelectionPath(path);
}
}
@Override
public void addSelectionPaths(TreePath[] paths) {
paths = getFilteredPaths(paths);
selectionModel.addSelectionPaths(paths);
}
@Override
public void removeSelectionPath(TreePath path) {
selectionModel.removeSelectionPath(path);
}
@Override
public void removeSelectionPaths(TreePath[] paths) {
selectionModel.removeSelectionPaths(paths);
}
@Override
public TreePath getSelectionPath() {
return selectionModel.getSelectionPath();
}
@Override
public TreePath[] getSelectionPaths() {
return selectionModel.getSelectionPaths();
}
@Override
public int getSelectionCount() {
return selectionModel.getSelectionCount();
}
@Override
public boolean isPathSelected(TreePath path) {
return selectionModel.isPathSelected(path);
}
@Override
public boolean isSelectionEmpty() {
return selectionModel.isSelectionEmpty();
}
@Override
public void clearSelection() {
selectionModel.clearSelection();
}
@Override
public void setRowMapper(RowMapper newMapper) {
selectionModel.setRowMapper(newMapper);
}
@Override
public RowMapper getRowMapper() {
return selectionModel.getRowMapper();
}
@Override
public int[] getSelectionRows() {
return selectionModel.getSelectionRows();
}
@Override
public int getMinSelectionRow() {
return selectionModel.getMinSelectionRow();
}
@Override
public int getMaxSelectionRow() {
return selectionModel.getMaxSelectionRow();
}
@Override
public boolean isRowSelected(int row) {
return selectionModel.isRowSelected(row);
}
@Override
public void resetRowSelection() {
selectionModel.resetRowSelection();
}
@Override
public int getLeadSelectionRow() {
return selectionModel.getLeadSelectionRow();
}
@Override
public TreePath getLeadSelectionPath() {
return selectionModel.getLeadSelectionPath();
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
selectionModel.addPropertyChangeListener(listener);
}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
selectionModel.removePropertyChangeListener(listener);
}
@Override
public void addTreeSelectionListener(TreeSelectionListener x) {
selectionModel.addTreeSelectionListener(x);
}
@Override
public void removeTreeSelectionListener(TreeSelectionListener x) {
selectionModel.removeTreeSelectionListener(x);
}
}
protected void initUI() {
final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
fillTree(root, 5, "Some tree label");
final DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
tree.setSelectionModel(new MyTreeSelectionModel());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(tree));
f.setSize(400, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void fillTree(DefaultMutableTreeNode parent, int level, String label) {
for (int i = 0; i < 5; i++) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(label + " " + i);
parent.add(node);
if (level > 0) {
fillTree(node, level - 1, label);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTreeSelection().initUI();
}
});
}
}
+0
不錯,,,,,,,,,,,,,, – mKorbel
+0
非常感謝,解決了我的問題!!!非常感謝您的幫助! –
+1
爲什麼委託而不是直接擴展'DefaultTreeSelectionModel'? (少寫代碼) –
相關問題
- 1. 的XPath - 基於多孩子選擇選擇父節點
- 2. 如果選擇單個節點爲空,則創建單節點
- 3. JTree節點沒有從DefaultTreeModel中刪除
- 4. 從每個節點的XML中選擇不同的大孩子
- 5. 選擇偶數小孩不包括隱藏的小孩
- 6. 從節點選擇中選擇節點
- 7. 不能在TransportClient創建InetSocketTransportAddress:NoNodeAvailableException [配置的節點沒有-are可供選擇:[]
- 8. antlr從三種不同的可能選項創建子節點
- 9. 如何創建一個不可選的TreeView節點?
- 10. 使用Xpath選擇不同的節點
- 11. 用於選擇屬性節點而不是元素節點的jquery選擇器?
- 12. 的XPath選擇父母的孩子節點有其他節點文本
- 13. 匹配,但在孩子未知數的值選擇節點的節點
- 14. 選擇節點
- 15. 選擇節點
- 16. 是否可以在haml中創建可選的父節點
- 17. jsTree - 不要選擇禁用父節點的子節點嗎?
- 18. jQuery的選擇器 - 找到根節點的孩子
- 19. 我如何選擇特定的LINQ的孩子節點?
- 20. 不可能覆蓋子節點上的用戶選擇?
- 21. 測驗選擇題場的內容節點創建測驗
- 22. 從XML選擇節點,並創建一個新的XML
- 23. 樹視圖 - 選擇的節點樣式不出現選擇的節點
- 24. XSLT1選擇兄弟姐妹的孩子節點值
- 25. 一起選擇XML節點將所有的孩子
- 26. 你有多少個孩子有我選擇的節點?
- 27. 選擇肥皂XML節點的孩子.NET
- 28. 如何默認選擇extjs中的孩子節點
- 29. Umbraco,創建節點,可用節點類型列表
- 30. 使用長列表選擇器創建可點擊的菜單
你可能會需要提供你自己的[選擇模型(http://docs.oracle.com/javase/7/docs/api/ javax/swing/tree/TreeSelectionModel.html)可以確定是否應該進行選擇 – MadProgrammer
感謝您的幫助 –
對於那些只希望根節點不可選的人來說,在點擊監聽器中可以添加類似這在你的TreeSelectionListener :: valueChanged()中:if(node == null || node.isRoot()){return; }' – lfxgroove