1
A
回答
3
嘗試this
編輯與更多的解釋:你要根據你的MutableTreeNode樹模型。上面的鏈接是Sun教程的一個例子。
+0
我看到這個例子,但我無法找到它的源代碼,也不知道如何設置根節點或在運行時獲取根節點來使用它。 感謝您的關注。 – 2009-12-29 15:29:45
6
您需要有一個TreeModel和TreeNode的自定義實現,請參見下文。只需擴展LazyTreeModel並實現loadChildren()。你可以用一個Thread()來代替它 - 工作人員相當於你的日誌記錄工具 和WorkerManager.getInstance()。schedule(新的LoadNodesWorker())。 Runnable接口。
public abstract class LazyTreeModel extends DefaultTreeModel implements TreeWillExpandListener {
public LazyTreeModel(TreeNode root, JTree tree) {
super(root);
setAsksAllowsChildren(true);
tree.addTreeWillExpandListener(this);
tree.setModel(this);
}
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
LazyTreeNode node = (LazyTreeNode) event.getPath().getLastPathComponent();
if (node.isLoaded()) {
return;
}
setLoading(node, false);
WorkerManager.getInstance().schedule(new LoadNodesWorker(node));
}
public void reloadNode(String id) {
LazyTreeNode node = findNode(id);
if (node != null) {
node.setLoaded(false);
setLoading(node, true);
WorkerManager.getInstance().schedule(new LoadNodesWorker(node));
}
}
public void reloadParentNode(String id) {
LazyTreeNode node = findParent(id);
if (node != null) {
node.setLoaded(false);
setLoading(node, true);
WorkerManager.getInstance().schedule(new LoadNodesWorker(node));
}
}
public LazyTreeNode findParent(String id) {
LazyTreeNode node = findNode(id);
if (node != null && node.getParent() != null) {
return (LazyTreeNode) node.getParent();
}
return null;
}
public void loadFirstLevel() {
setLoading((LazyTreeNode) getRoot(), false);
WorkerManager.getInstance().schedule(new LoadNodesWorker((LazyTreeNode) getRoot()));
}
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
}
protected void setChildren(LazyTreeNode parentNode, LazyTreeNode... nodes) {
if (nodes == null) {
return;
}
int childCount = parentNode.getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
removeNodeFromParent((MutableTreeNode) parentNode.getChildAt(0));
}
}
for (int i = 0; i < nodes.length; i++) {
insertNodeInto(nodes[i], parentNode, i);
}
}
private void setLoading2(final LazyTreeNode parentNode, final boolean reload) {
if (reload) {
setChildren(parentNode, createReloadingNode());
} else {
setChildren(parentNode, createLoadingNode());
}
}
private void setLoading(final LazyTreeNode parentNode, final boolean reload) {
if (SwingUtilities.isEventDispatchThread()) {
setLoading2(parentNode, reload);
} else {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
setLoading2(parentNode, reload);
}
});
} catch (Exception e) {
LOG.error("Cannot create loading node", e);
}
}
}
private LazyTreeNode findNode(String id) {
return findNode(id, (LazyTreeNode) getRoot());
}
private LazyTreeNode findNode(String id, LazyTreeNode parent) {
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
LazyTreeNode node = (LazyTreeNode) parent.getChildAt(i);
if (id.equals(node.getId())) {
return node;
}
if (node.isLoaded()) {
node = findNode(id, node);
if (node != null) {
return node;
}
}
}
return null;
}
public abstract LazyTreeNode[] loadChildren(LazyTreeNode parentNode);
protected LazyTreeNode createLoadingNode() {
return new LazyTreeNode(null, "Loading...", false);
}
protected LazyTreeNode createReloadingNode() {
return new LazyTreeNode(null, "Refreshing...", false);
}
class LoadNodesWorker implements Worker {
private LazyTreeNode parentNode;
LoadNodesWorker(LazyTreeNode parent) {
this.parentNode = parent;
}
public String getName() {
return "Lazy loading of node " + parentNode.getId();
}
public void execute() throws Exception {
final LazyTreeNode[] treeNodes = loadChildren(parentNode);
if (treeNodes == null) {
return;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
parentNode.setLoaded(true);
setChildren(parentNode, treeNodes);
}
});
}
}
}
public class LazyTreeNode extends DefaultMutableTreeNode {
private boolean loaded;
private String id;
public LazyTreeNode(String id) {
this(id, null);
}
public LazyTreeNode(String id, Object userObject) {
this(id, userObject, true);
}
public LazyTreeNode(String id, Object userObject, boolean allowsChildren) {
super(userObject, allowsChildren);
this.id = id;
}
public String getId() {
return id;
}
protected boolean isLoaded() {
return loaded;
}
protected void setLoaded(boolean loaded) {
this.loaded = loaded;
}
@Override
public boolean isLeaf() {
return !getAllowsChildren();
}
}
1
通過I82Much提到的孫實例教程,包括示範項目,DynamicTreeDemo,即由兩個源文件DynamicTreeDemo.java和DynamicTree.java的。你應該可以從這些鏈接中找到他們。
相關問題
- 1. JTree的動態顯示?
- 2. JTree動態節點插入
- 3. 如何動態填充JTree?
- 4. Java-更新JTree
- 5. 更改JTree的節點圖像動態
- 6. 動態JTree由JTextField中的KeyListener更新
- 7. JTree動態更新與arraylist更改
- 8. 在JTree中動態添加節點
- 9. 動態在JTree節點添加
- 10. java JTree不更新
- 11. java定製JTree insertnodeinto
- 12. 我如何根據我的動態變化Jtree爲java桌面應用程序更改Jtree的顏色
- 13. 在Java GUI中更新JTree
- 14. (JAVA)從JTree的到XML
- 15. 的Java Swing JTree的擴展
- 16. xml jtree解析在java
- 17. 如何從在java中動態創建的JTree生成XML文件
- 18. Java JTree:清除選擇狀態(重置參考)
- 19. 節點動態添加到非葉節點不工作| JTree
- 20. 動態更改JTree中特定節點的圖標
- 21. 如何將JTree動態添加到創建的JScrollPane?
- 22. 從JTree中的文本文件動態填充樹模型
- 23. 動態的JTree和SwingUtilities.invokeLater()什麼也不做
- 24. 如何使用JTree Listener使用CardLayout動態更改JPanel?
- 25. JDialog - 在JTree中刷新動態添加的節點
- 26. Java的禁用切紙動作JTree的/的TransferHandler
- 27. 重命名JTree的節點手動將在Java中
- 28. Java動態Jtable
- 29. 的Java刪除focusPainted上DefaultMutableTreenNode在JTree的
- 30. MouseEvent之前的Java JTree valueChanged事件
您不能動態地將節點添加到JTree,因爲JTree是視圖部件,而不是模型部件。你需要分離和跟蹤你的模型(Adrian發佈了一個好的模型來使用),然後你可以動態地將節點添加到你的模型中。 – Pace 2009-12-29 17:01:52