简体   繁体   English

Java JTree 获取所选节点的名称

[英]Java JTree get name of selected node

I would like to find out how to get the name of the selected node in a JTree, not the userObject which is the text of the treenode.我想了解如何获取 JTree 中所选节点的名称,而不是作为树节点文本的 userObject。

I have manually created several tree nodes and I need to determine which one is selected by the node's name.我手动创建了几个树节点,我需要确定通过节点名称选择了哪一个。

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); 
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Hey it's node1!");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Hey, it's node2!");
root.add(node1); root.add(node2); 

Now, based on that, I need to know if node1 or node2 is selected.现在,基于此,我需要知道是否选择了 node1 或 node2。 I want a return value of node1 or node2 (variable name).我想要 node1 或 node2 (变量名称)的返回值。

To make this more clear: I have a JFrame, with a JPanel, a JScrollPane, a JTree, and some text fields.为了更清楚地说明这一点:我有一个 JFrame,带有一个 JPanel、一个 JScrollPane、一个 JTree 和一些文本字段。 Depending on which node is clicked (node 1, or node 2), I want to show or hide certain text fields.根据单击的节点(节点 1 或节点 2),我想显示或隐藏某些文本字段。

If I wanted my Node to have a name, I'd extend the base class, here DefaultMutableTreeNode, and give it a String name field, and then fill that field with a value via a constructor, one that takes both a String name parameter and a String text parameter.如果我想让我的 Node 有一个名字,我会扩展基类,这里是 DefaultMutableTreeNode,并给它一个 String name 字段,然后通过构造函数用一个值填充该字段,一个同时接受 String name 参数和字符串文本参数。 And then also give it a getter method.然后也给它一个getter方法。

I wouldn't try to get its "variable" name since that is close to Meaningless.我不会尝试获得它的“变量”名称,因为它接近无意义。 Many variables don't even have "names", that an object can be referenced by 2, 3, 100, ... variables.许多变量甚至没有“名称”,即一个对象可以被 2、3、100、...变量引用。


Edit编辑
You state in an edit:您在编辑中声明:

To make this more clear: I have a JFrame, with a JPanel, a JScrollPane, a JTree, and some text fields.为了更清楚地说明这一点:我有一个 JFrame,带有一个 JPanel、一个 JScrollPane、一个 JTree 和一些文本字段。 Depending on which node is clicked (node 1, or node 2), I want to show or hide certain text fields.根据单击的节点(节点 1 或节点 2),我想显示或隐藏某些文本字段。

Again, variable names are close to meaningless and almost don't exist in compiled code.同样,变量名几乎毫无意义,并且几乎不存在于编译代码中。 What counts are variable references -- being able to grab a variable, and object state -- the state of the fields held by that variable.重要的是变量引用——能够获取一个变量和对象状态——该变量所持有的字段的状态。

Kind of old that thread, but in my opinion you do not have to derive any class.该线程有点旧,但在我看来,您不必派生任何类。 When you initialize a DefaultMutableTreeNode object with the optional parameter userObj, you can get this parameter again by calling getUserObject().当您使用可选参数 userObj 初始化 DefaultMutableTreeNode 对象时,您可以通过调用 getUserObject() 再次获取此参数。 See the api for further information: https://docs.oracle.com/javase/10/docs/api/javax/swing/tree/DefaultMutableTreeNode.html有关更多信息,请参阅 api: https : //docs.oracle.com/javase/10/docs/api/javax/swing/tree/DefaultMutableTreeNode.html

I think is is pretty much what you are searching for - at least I was searching for it.我认为这几乎是您正在寻找的东西 - 至少我正在寻找它。

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

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