我在这里搜索的东西有点迷失,因为我似乎无法找到任何东西。
基本上我使用JTree
来显示服务器及其中的结构。我不想加载整个结构,因为查询需要很长时间,因此我希望在用户扩展节点时加载它。
结构看起来像这样(我总是知道根服务器节点)
服务器 -tables -Columns
我想首先添加' Server',然后当用户展开它时,我想添加' Tables',然后当用户扩展它时我想添加' Columns'。
我已经按照设置整个TreeWillExpandListener的教程,这是我的代码:
public class ServerViewer extends JScrollPane
implements TreeModelListener, TreeExpansionListener, TreeWillExpandListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public ServerViewer(
final Model model) {
Validate.isTrue(model != null, "'model' must not be null.");
this.model = model;
// Create root node of tree
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Servers (2)");
createNodes(rootNode);
// Create tree model
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(this);
// Create tree
this.browserTree = new JTree(treeModel);
//this.browserTree.setRootVisible(false);
this.browserTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.browserTree.addTreeExpansionListener(this);
this.browserTree.addTreeWillExpandListener(this);
// Add components
this.getViewport().add(this.browserTree);
}
/**
* Populates the root node with the expected default nodes.
*
* @param rootNode The node to populate. Must not be null.
*/
private void createNodes(DefaultMutableTreeNode rootNode) {
Validate.isTrue(rootNode != null, "'rootNode' must not be null.");
for (ServerModel server: this.model.getServers()) {
DefaultMutableTreeNode serverNode = new DefaultMutableTreeNode(server, true);
rootNode.add(serverNode);
}
}
/**
* @return The tree which this uses as a browser. Will not be null.
*/
public JTree getBrowserTree() {
return this.browserTree;
}
@Override
public void treeNodesChanged(
TreeModelEvent arg0) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)arg0.getTreePath().getLastPathComponent();
/*
* If the event lists children, then the changed
* node is the child of the node we have already
* gotten. Otherwise, the changed node and the
* specified node are the same.
*/
try {
int index = arg0.getChildIndices()[0];
node = (DefaultMutableTreeNode)node.getChildAt(index);
}
catch (NullPointerException exc) {}
System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
@Override
public void treeNodesInserted(
TreeModelEvent arg0) {
}
@Override
public void treeNodesRemoved(
TreeModelEvent arg0) {
}
@Override
public void treeStructureChanged(
TreeModelEvent arg0) {
}
@Override
public void treeCollapsed(TreeExpansionEvent arg0) {
}
@Override
public void treeExpanded(TreeExpansionEvent arg0) {
}
@Override
public void treeWillCollapse(
TreeExpansionEvent arg0) throws ExpandVetoException {
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("WillCollapse: " + data);
}
@Override
public void treeWillExpand(
TreeExpansionEvent arg0) throws ExpandVetoException {
TreePath path = arg0.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
String data = node.getUserObject().toString();
System.out.println("WillExpand: " + data);
}
/**
* The model which this is viewing.
*/
private Model model;
/**
* Displays the contents of a database.
*/
private JTree browserTree;
}
我的问题是当我把“服务器”放在一起时。节点在,它没有子节点,所以它没有控件来扩展它。这意味着' treeWillExpand()'永远不会为该节点调用。我怎样才能使它具有扩展控制?
此外,在这里使用的正确术语是什么,因为它似乎是一个非常典型的事情,但我无法在Google上找到任何内容。
答案 0 :(得分:2)
根据aterai的回答:
treeModel.setAsksAllowsChildren(true)