Bhushit Agarwal
Bhushit Agarwal

Reputation: 21

Adding custom property to JTree and also print it

I'm trying to implement a FP-Tree. So I have used a JTree. I have created my tree successfully. But I need to add two custom properties to my nodes, 'Label' and 'LabelCount'

And I also need to print it in the tree. Is that possible?

Upvotes: 1

Views: 314

Answers (1)

bluevoid
bluevoid

Reputation: 1359

Use DefaultMutableTreeNode as nodes and add a user object to that, than add your own implementation of TreeCellRenderer to the tree (setTreecellRenderer(...)
In your TreeCellRenderer implement the getTreeCellRendererComponent method.

Component getTreeCellRendererComponent(JTree tree,
                                 Object value,
                                 boolean selected,
                                 boolean expanded,
                                 boolean leaf,
                                 int row,
                                 boolean hasFocus)

The Object value argument will be your DefaultMutableTreeNode which contains your user object.

You can just return a JLabel (or any other Component) that contains the text you want.

For performance reasons you can reuse your JLabel, no need to create a new one every time the method is called.

Upvotes: 1

Related Questions