Make components in tree nodes clickable

Hi

I created a tree and inserted nodes into the treeModel

Then I render the cells using DefaultTreeCellRenderer and add my custom components like labels and buttons

The problem is, these components are not reacting to mouse clicks

How to make them clickable?

Video: https://cln.sh/SJNZrrst

Here's the code I use to create the tree

private val myTree: Tree = Tree().apply {
  model = treeModel
  cellRenderer = ValidatorDefaultTreeCellRenderer()
}

Part of the code to create the button for example

class ValidatorDefaultTreeCellRenderer : DefaultTreeCellRenderer() {
    override fun getTreeCellRendererComponent(
        tree: JTree?,
        value: Any?,
        sel: Boolean,
        expanded: Boolean,
        leaf: Boolean,
        row: Int,
        hasFocus: Boolean
    ): Component {

        return JBPanel<JBPanel<*>>().apply {
            add(JButton().apply {
                text = "TEST"
                addMouseListener(object : MouseAdapter() {
                    override fun mouseClicked(e: MouseEvent) {
                        FileHelper.navigateToFile(userObject)
                    }
                })
            })
        }

    }
}

I know that I can attach a click listener to the tree itself but it was cumbersome to detect the clicked element and figure out what to do

Any hint is appreciated 

Thank you very much

0

Please sign in to leave a comment.