简体   繁体   English

由KeyListener在JTextField中更新的动态JTree

[英]Dynamic JTree updated by KeyListener in a JTextField

I would like my JTree to be dynamically updated by a user initiated search (over elements of the tree). 我希望通过用户启动的搜索(在树的元素上)动态更新JTree。 From output on the console I can tell, that the search works like it should. 从控制台上的输出中,我可以看出,搜索的工作方式应该像它应该的那样。 The problem is updating the tree. 问题是更新树。

Here is what I got. 这就是我得到的。 I have a set of classes like 我有一组像

public class classA {

    int id;

    String name;

    List<ClassB> listOfClassB;
}

public class classB {

    int id;

    String name;

    List<ClassC> listOfClassC;
}

public class classC {

    int id;

    String name;
}

From these classes I generate a JTree by looping throught the classes lists in createTree(). 通过遍历createTree()中的类列表,从这些类中生成一个JTree。

private DefaultMutableTreeNode rootNode;
rootNode = createTree("New", ""); // "New" tells the method to generate the complete tree
treeModel = new DefaultTreeModel(rootNode);
JTree myTree = new JTree(treeModel);

So far so good. 到现在为止还挺好。 Now I want to search the tree for a String or Int in classC.name or classC.id and "remove" all nodes that do not match the search criteria. 现在,我想在树中搜索classC.name或classC.id中的String或Int,然后“删除”所有不符合搜索条件的节点。 But not the objects, only the corresponding tree nodes. 但不是对象,只有相应的树节点。 So that when the user is done searching I can show the whole tree again. 这样,当用户完成搜索后,我可以再次显示整个树。

The search is implemented with JTextField and a KeyListener that calls createTree(searchOption, searchText). 搜索是通过JTextField和调用createTree(searchOption,searchText)的KeyListener实现的。

So far I have tried to remove all nodes with removeAllChildren()and then add new nodes that match the search criteria. 到目前为止,我已经尝试使用removeAllChildren()删除所有节点,然后添加与搜索条件匹配的新节点。 Reloading the TreeModel afterwards does not seem to work here. 之后,重新加载TreeModel在这里似乎不起作用。

createTree(searchOption, searchText);
rootNode.removeAllChildren();
treeModel.reload(rootNode);

Any ideas how I could accomplish that? 有什么想法可以实现吗?

PS: I chose to delete the tree and generate a new one to avoid keeping the tree and the data in sync because it seems way more complicated. PS:我选择删除树并生成一个新树,以避免使树和数据保持同步,因为它看起来更加复杂。

JTree myTree = new JTree(treeModel);

That statement doesn't look correct. 那句话看起来不正确。 You create a new JTree but I don't see where you add the tree back to the frame. 您创建了一个新的JTree,但是我看不到将树添加回框架的位置。

Instead of creating a new tree, just resetting the model of the tree: 无需创建新树,只需重置树的模型即可:

myTree.setModel( treeModel );

The search is implemented with JTextField and a KeyListener that calls createTree(searchOption, searchText). 搜索是通过JTextField和调用createTree(searchOption,searchText)的KeyListener实现的。

Don't use a KeyListener. 不要使用KeyListener。 You should be using a DocumentListener to listen for changes in text. 您应该使用DocumentListener来侦听文本中的更改。 Read the section from the Swing tutorial on Listener For Change on a Document . 阅读Swing教程中有关文档更改的侦听器的部分。

I did manage to load a "new" tree by creating and setting a new root node before reloading the model. 在重新加载模型之前,我确实通过创建和设置新的根节点来加载“新”树。 It does not seem "clean" to me, but it did the trick. 在我看来,这似乎并不“干净”,但确实可以解决问题。

    rootNode = createTree(searchOption, searchText);
    treeModel.setRoot(rootNode);
    treeModel.reload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM