简体   繁体   中英

JTree How To Change A Node programmatically?

I'm trying for the first time to use and understand JTrees, TreeModels, TreeModes etc. I want to use a Tree to represent Bluetooth devices in the vicinity of my local device. My Bluetooth device is the Root node of the tree and any FIRST descendents from the root node will be Bluetooth devices in the vicinity.

I set up my Tree to begin with as follows (code shortened for brevity):

topTreeNode = new DefaultMutableTreeNode("Local Bluetooth Device");
treeModel = new DefaultTreeModel(topTreeNode);
jTree1 = new javax.swing.JTree(treeModel);

When the JTree is initially displayed the root node reads 'Local Bluetooth Device'. However once I initialise the Bluetooth stack I'd like the root node to display the 'friendly name' and address of my Local bluetooth device.

The problem I have got is just how do I change the root node? I have tried a variety of methods including creating a new DefaultMutableTreeNode() for topTreeNode but the JTree doesn't re-draw straight away. Am I supposed to implement a listener?

as requested, posting this as an answer:

i think invoking getUserObject() on the root node should get you what you created the node with. If its a custom object and its mutable, you can then set its value :-) Or just setUserObject() could allow you to change it anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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