双击FileTreeModel中的文件

时间:2013-04-18 06:40:29

标签: java swing

我对Java很满意,但是要使用Java。谷歌搜索了一段时间后,到目前为止我的程序工作正常。现在我想要的是,双击树中的任何文件,在下面的函数执行。

有人可以建议我如何添加听众来理解双击事件?示例真的会有所帮助。

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

我的工作程序如下,

/*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);
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您应该添加MouseListener并实施mousePressed方法

MouseListener ml = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if(e.getClickCount() == 2) {
             getSomeData(...);
        }
    }
};
tree.addMouseListener(ml);