如何从TreeSelectionListener获取路径

时间:2012-11-15 19:09:19

标签: file jtable listener jtree

这是我的问题。我想写一个原始的MultiRenameTool(重命名部分还不重要)。 您可以浏览左侧的目录(JTree),当您选择一个时,您可以在右侧看到它的内容(JTable - 只是文件)。

snapshot

问题是,我无法弄清楚如何将所选目录传递给JTable的列表。

我使用了Kirill Grouchnikov的代码用于JTree,我稍微修改了它。

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.io.FileFilter;
import java.util.*;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 * @author Kirill Grouchnikov
 */

public class FileTreePanel extends JPanel {

    protected static FileSystemView fsv = FileSystemView.getFileSystemView();
    private JTree tree;
    //At first I was trying this - but it is wrong.
    public static File current;

    private static class FileTreeCellRenderer extends DefaultTreeCellRenderer {

    private Map<String, Icon> iconCache = new HashMap<String, Icon>();
    private Map<File, String> rootNameCache = new HashMap<File, String>();

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        FileTreeNode ftn = (FileTreeNode) value;
        File file = ftn.file;
        String filename = "";
        if (file != null) {
        if (ftn.isFileSystemRoot) {
            filename = this.rootNameCache.get(file);
            if (filename == null) {
            filename = fsv.getSystemDisplayName(file);
            this.rootNameCache.put(file, filename);
            }

        } else {
            filename = file.getName();
        }
        }
        JLabel result = (JLabel) super.getTreeCellRendererComponent(tree, filename, sel, expanded, leaf, row, hasFocus);
        if (file != null) {
        Icon icon = this.iconCache.get(filename);
        if (icon == null) {
            icon = fsv.getSystemIcon(file);
            this.iconCache.put(filename, icon);
        }
        result.setIcon(icon);
        }
        return result;
    }
    }

    private static class FileTreeNode implements TreeNode {

    private File file;
    private File[] children;
    private TreeNode parent;
    private boolean isFileSystemRoot;

    public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
        this.file = file;
        this.isFileSystemRoot = isFileSystemRoot;
        this.parent = parent;
        this.children = this.file.listFiles(new FileFilter() {
        //!Modification here - display only the directories
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
        });
        if (this.children == null) {
        this.children = new File[0];
        }
        //obliviously wrong "solution" :(
        current = file;
    }

    public FileTreeNode(File[] children) {
        this.file = null;
        this.parent = null;
        this.children = children;
    }

    @Override
    public Enumeration<?> children() {
        final int elementCount = this.children.length;
        return new Enumeration<File>() {
        int count = 0;

        @Override
        public boolean hasMoreElements() {
            return this.count < elementCount;
        }

        @Override
        public File nextElement() {
            if (this.count < elementCount) {
            return FileTreeNode.this.children[this.count++];
            }
            throw new NoSuchElementException("Nincs több elem.");
        }
        };
    }

    @Override
    public boolean getAllowsChildren() {
        return true;
    }

    @Override
    public TreeNode getChildAt(int childIndex) {
        return new FileTreeNode(this.children[childIndex],
            this.parent == null, this);
    }

    @Override
    public int getChildCount() {
        return this.children.length;
    }

    @Override
    public int getIndex(TreeNode node) {
        FileTreeNode ftn = (FileTreeNode) node;
        for (int i = 0; i < this.children.length; i++) {
        if (ftn.file.equals(this.children[i])) {
            return i;
        }
        }
        return -1;
    }

    @Override
    public TreeNode getParent() {
        return this.parent;
    }

    @Override
    public boolean isLeaf() {
        return (this.getChildCount() == 0);
    }
    }

    public FileTreePanel() {
    super();
    this.setLayout(new BorderLayout());

    File[] roots = File.listRoots();

    FileTreeNode rootTreeNode = new FileTreeNode(roots);
    this.tree = new JTree(rootTreeNode);
    this.tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);


    this.tree.setCellRenderer(new FileTreeCellRenderer());
    this.tree.setRootVisible(true);
    this.tree.addTreeSelectionListener(new JTreeSelectionListener());

    JScrollPane jsp = new JScrollPane(this.tree);
    this.add(jsp, BorderLayout.CENTER);
    }

    private class JTreeSelectionListener implements TreeSelectionListener {

    @Override
    public void valueChanged(TreeSelectionEvent jtsl) {
        TreePath o = jtsl.getPath();
        System.out.println(o);
        System.out.println(current);

        SelectionList.listBuilder(current);
    }
    }
}

最重要的部分是在TreeSelectionEvent的最后。在某种程度上,我应该能够从实际选择的目录中转换/制作/转换文件。

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.io.FileFilter;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class SelectionList extends JPanel {

    private static FilesData data;
    private static JTable table;

    public SelectionList() {
    super();
    data = new FilesData();
    table = new JTable(data);

    table.setFillsViewportHeight(true);
    table.setShowGrid(true);
    table.setGridColor(Color.BLACK);

    JScrollPane jsp = new JScrollPane(table);
    this.add(jsp, BorderLayout.CENTER);

    }

    public static void listBuilder(final File f) {
    data.files.clear();
    File[] fs = f.listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
        return file.isFile();
        }
    });
    if (fs != null) {
        for (File m : fs) {
        Files ujFile = new Files(m, m.isHidden(), Menu.checkAll.getState());
        if (!m.isHidden() || Menu.hiddenFilesVisibility.getState()) {
            data.files.add(ujFile);
        }
        }
    }
    table.repaint();
    }
}

因为listBuilder函数很有趣。我认为这是足够的信息,但如果你也需要我的其他课程,我会上传它。我感谢任何帮助!谢谢大家。任何人? :(

1 个答案:

答案 0 :(得分:1)

以下代码段显示了如何从valueChanged

中的路径获取文件
public void valueChanged(TreeSelectionEvent jtsl) {

    TreePath path = jtsl.getPath();
    FileTreeNode filenode = (FileTreeNode) path.getLastPathComponent();
    File file = filenode.file;

    ...

}