I have a program that has a JTree that lists of all the directories on my computer. I also have a JList in and an add button. Now I am trying to select or highlight a directory in the JTree, click the add Button and then update the JList with the name of the node (Directory) I have clicked. I have looked at mouse listeners actionlisteners and tree selectionListeners but cannot come up with anything that seems feasible.

Any suggestions may be helpful


Assuming you only allow a single selection in the JTree you can use: JTree.getSelectionPath.

It returns a TreePath which basically holds a list of the hierarchy that is selected. Then you can use TreePath.getLastPathComponent to get the name of the node you're looking for.


commented: Helpful answer. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.