简体   繁体   中英

How to get components rendered inside JTree (Swing Component Hierachy)

For some specific purpose I am trying to manipulate UI by just accessing Swing/AWT component hierarchy, and I am stuck at JTree.

I have a JTree which is rendered using a custom TreeCellRenderer, and have JCheckBox in each node.

样本JTree

I am trying to obtain a specific JCheckBox object used to render a specific node, to programmatically change it's state, what I have is JTree Component object.

I couldn't find a way to get the JCheckBox component created to render a specific node.

If I keep calling Container.getComponents() recursively, I reach upto :

class javax.swing.JTree
class javax.swing.CellRendererPane
class javax.swing.tree.DefaultTreeCellRenderer

but these doen't seems to giving out components rendered by custom renderers.

As @JB-Nizet pointed out in comment, I solved my problem by updating custom model object using reflection.

Object modelObject = nodeToSelect.getUserObject();
Method method = modelObject.getClass().getMethod("setSelected", boolean.class);
method.invoke(modelObject,true);

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