1

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?

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • 3
    The DefaultMutableTreeNode takes a "user object" (which it uses as the default value for rendering the node). Your best bet would be to create a simple Property object and applying to each node – MadProgrammer Mar 30 '13 at 19:46

1 Answers1

1

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.

bluevoid
  • 1,274
  • 13
  • 29