Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

How to find selected Nodes of JTree used TreeListener any other way.

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is a class which extends JTree class forms a Tree displaying employee information.Listener is written which should find the list of selected Employee nodes of tree component.

The method valueChanged() of treeListener is getting called twice for every node selected. Please find the code.

Is there any alternative to find the list of selected nodes when they are selected using mouse or shift key.

Even I am facing this in case of row selection of JTable.The listener method is called twice. Could you please let me know your views and any tutorials or examples codes for handling such cases.

public class TestingTreeModel extends JTree {

//public vector myMOType;

public TestingTreeModel() {

public void setData(ArrayList testVOs) {

// set the option of selecting multiple nodes

setModel(rootTreeNode, testVOs);

// DefaultTreeModel is used.
this.setModel(new DefaultTreeModel(rootTreeNode));

// Listener for selected nodes..
MyTreeSelectionListener myTreeListener = new MyTreeSelectionListener();


// which sets the data.

public void setModel(DefaultMutableTreeNode rootNode, ArrayList empVOs) {
if empVOs!= null && empVOs.size() > 0) {
Iterator empItr = empVOs.iterator();
while (neItr.hasNext()) {
Employee empVO = (Employee);
DefaultMutableTreeNode node =
new DefaultMutableTreeNode(empVO.getName());


// Listener

public class MyTreeSelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent evt) {
// Get all nodes whose selection status has changed
System.err.println("Listener is called here check how many times");

private void getSelectedNodes(TreeSelectionEvent evt) {

System.err.println("came inside getSelectedNodes");

TreePath[] paths = evt.getPaths();
// Iterate through all affected nodes

System.err.println(" Length is " + paths.length);

for (int i = 0; i < paths.length; i++) {
TreePath tp = paths;
Object[] o = tp.getPath();
for (int j = 0; j < o.length; j++) {
"Class: " + o[j].getClass() + "; value: " + o[j]);
if (o[j] instanceof DefaultMutableTreeNode) {
Object uo = ((DefaultMutableTreeNode) o[j]).getUserObject();
if (uo != null) {
"\tUser object class: "
+ uo.getClass()
+ "; value: "
+ uo);
private ArrayList empIDList = new ArrayList();
private DefaultMutableTreeNode rootTreeNode = null;
Posts: 976
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well your tree selection listener will be called
both when selections are added and when selections
are deleted, so that could be your problem.

Your code doesn't compile as is, but you don't seem
to be doing anything like this:
Posts: 27211
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to find what nodes are selected, you don't have to write a listener to keep track. The tree's selection model is already keeping track and you just have to ask it:
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic