0

I am comfortable with Java but to swing API. After googling for sometime, I have got below program working fine so far. Now what I want is, on double clicking on any file in the tree, below function to be executed.

Can someone suggest me how can I add a listener to understand double click event? Example would really help.

public boolean getSomeData(String fileName){
  //I will make JDBC call here    
}

My working program is as below,

/*http://www.chka.de/swing/tree/FileTreeModel.html
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.EventListenerList;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;



public class FileTreeModel
    implements TreeModel, Serializable, Cloneable
{
    protected EventListenerList listeners;

    private static final Object LEAF = new Serializable() { };

    private Map map;



    private File root;


    public FileTreeModel(File root)
    {
        this.root = root;

        if (!root.isDirectory())
            map.put(root, LEAF);

        this.listeners = new EventListenerList();

        this.map = new HashMap();
    }


    public Object getRoot()
    {
        return root;
    }

    public boolean isLeaf(Object node)
    {
        return map.get(node) == LEAF;
    }



    public int getChildCount(Object node)
    {
        List children = children(node);

        if (children == null)
            return 0;

        return children.size();
    }



    public Object getChild(Object parent, int index)
    {
        return children(parent).get(index);
    }

    public int getIndexOfChild(Object parent, Object child)
    {
        return children(parent).indexOf(child);
    }


    protected List children(Object node)
    {
        File f = (File)node;

        Object value = map.get(f);

        if (value == LEAF)
            return null;

        List children = (List)value;

        if (children == null)
        {
            File[] c = f.listFiles();

            if (c != null)
            {
                children = new ArrayList(c.length);

                for (int len = c.length, i = 0; i < len; i++)
                {
                    children.add(c[i]);
                    if (!c[i].isDirectory())
                        map.put(c[i], LEAF);
                }
            }
            else
                children = new ArrayList(0);

            map.put(f, children);      
        }

        return children;
    }



    public void valueForPathChanged(TreePath path, Object value)
    {
    }


    public void addTreeModelListener(TreeModelListener l)
    {
        listeners.add(TreeModelListener.class, l);
    }

    public void removeTreeModelListener(TreeModelListener l)
    {
        listeners.remove(TreeModelListener.class, l);
    }

    public Object clone()
    {
        try
        {
            FileTreeModel clone = (FileTreeModel)super.clone();

            clone.listeners = new EventListenerList();

            clone.map = new HashMap(map);

            return clone;
        }
        catch (CloneNotSupportedException e)
        {
            throw new InternalError();
        }
    }


    public static void main(String[] args)
    {
        if (args.length != 1)
        {
            System.err.println("Usage: java FileTreeModel path");
            System.exit(1);
        }

        File root = new File(args[0]);

        if (!root.exists())
        {
            System.err.println(root+ ": No such file or directory");
            System.exit(2);
        }

        JTree tree = new JTree(new FileTreeModel(root));

        JFrame f = new JFrame(root.toString());

        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        f.getContentPane().add(new JScrollPane(tree));

        f.pack();
        f.setVisible(true);
    }
}
Sachin
  • 449
  • 2
  • 9
  • 27

1 Answers1

1

I think you should add MouseListener and implement the mousePressed method

MouseListener ml = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if(e.getClickCount() == 2) {
             getSomeData(...);
        }
    }
};
tree.addMouseListener(ml);
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • As suggested in the [`JTree API`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTree.html); more [here](http://stackoverflow.com/q/15625424/230513). – trashgod Apr 18 '13 at 10:36