JTree首次加载时未显示句柄 [英] JTree isn't showing handles when it first loads up

查看:68
本文介绍了JTree首次加载时未显示句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此对于我正在从事的项目,我需要来自文件管理器的文件树,该文件树显示系统中的所有目录和文件.我找到了一个完整的工作文件管理器,名为FileManager.Java,可以在此页面上看到: https://codereview .stackexchange.com/questions/4446/file-browser-gui

So for a project I'm working on, I need the file tree from a file manager that displays all of the directories and files from a system. I found a full working file manager called FileManager.Java which can be seen on this page: https://codereview.stackexchange.com/questions/4446/file-browser-gui

但是,正如我所说,我只需要树的一部分.我已经实现了这一点,并采用了FileManager.java代码并将其分解为我认为必要的代码.该代码在这里:

However, as I said, I only need the tree part of this. I have already achieved this, and took the FileManager.java code and broke it down to what I believed necessary. That code is here:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.filechooser.FileSystemView;

import java.util.List;
import java.io.*;


class FileManager {

    /** Provides nice icons and names for files. */
    private FileSystemView fileSystemView;

    /** File-system tree. Built Lazily */
    private JTree tree;
    private DefaultTreeModel treeModel;

    /** Directory listing */
    private JProgressBar progressBar;

    public Container getGui() {
        fileSystemView = FileSystemView.getFileSystemView();

        // the File tree
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        treeModel = new DefaultTreeModel(root);

        TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent tse){
                DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
                showChildren(node);
            }
        };

        // show the file system roots.
        File[] roots = fileSystemView.getRoots();
        for (File fileSystemRoot : roots) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
            root.add( node );
            //showChildren(node);
            File[] files = fileSystemView.getFiles(fileSystemRoot, true);
            for (File file : files) {
                if (file.isDirectory()) {
                    node.add(new DefaultMutableTreeNode(file));
                }
            }
        }

        tree = new JTree(treeModel);
        tree.setRootVisible(false);
        tree.addTreeSelectionListener(treeSelectionListener);
        tree.setCellRenderer(new FileTreeCellRenderer());
        tree.expandRow(0);
        tree.setVisibleRowCount(15);

        JPanel simpleOutput = new JPanel(new BorderLayout(3,3));
        progressBar = new JProgressBar();
        simpleOutput.add(progressBar, BorderLayout.EAST);
        progressBar.setVisible(false);

        return tree;
    }

    public void showRootFile() {
        // ensure the main files are displayed
        tree.setSelectionInterval(0,0);
    }




    /** Add the files that are contained within the directory of this node.
    Thanks to Hovercraft Full Of Eels. */
    private void showChildren(final DefaultMutableTreeNode node) {
        tree.setEnabled(false);
        progressBar.setVisible(true);
        progressBar.setIndeterminate(true);

        SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
            @Override
            public Void doInBackground() {
                File file = (File) node.getUserObject();
                if (file.isDirectory()) {
                    File[] files = fileSystemView.getFiles(file, true); //!!
                    if (node.isLeaf()) {
                        for (File child : files) {
//                            if (child.isDirectory()) {
                                publish(child);
//                           }
                        }
                    }
                }
                return null;
            }

            @Override
            protected void process(List<File> chunks) {
                for (File child : chunks) {
                    node.add(new DefaultMutableTreeNode(child));
                }
            }

            @Override
            protected void done() {
                progressBar.setIndeterminate(false);
                progressBar.setVisible(false);
                tree.setEnabled(true);
            }
        };
        worker.execute();
    }
}


/** A TreeCellRenderer for a File. */
class FileTreeCellRenderer extends DefaultTreeCellRenderer {

    private FileSystemView fileSystemView;

    private JLabel label;

    FileTreeCellRenderer() {
        label = new JLabel();
        //label.setOpaque(true);
        fileSystemView = FileSystemView.getFileSystemView();
    }

    @Override
    public Component getTreeCellRendererComponent(
        JTree tree,
        Object value,
        boolean selected,
        boolean expanded,
        boolean leaf,
        int row,
        boolean hasFocus) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        File file = (File)node.getUserObject();
        label.setIcon(fileSystemView.getSystemIcon(file));
        label.setText(fileSystemView.getSystemDisplayName(file));
        label.setToolTipText(file.getPath());

        if (selected) {
            label.setBackground(backgroundSelectionColor);
            label.setForeground(textSelectionColor);
        } else {
            label.setBackground(backgroundNonSelectionColor);
            label.setForeground(textNonSelectionColor);
        }

        return label;
    }
}

当我在实际程序中调用它时,除目录句柄外,其他所有东西都工作正常.我不知道如何在程序启动后立即显示它们.由于某种原因,您必须先单击该目录才能显示它.如果有人有任何想法,将不胜感激.

When I call this into my actual program, everything works fine, except for the handles for the directories. I can't figure out how to get them to display as soon as the program starts. For some reason, you have to click on the directory first in order for it to show up. If anyone has any ideas, that'd be greatly appreciated.

如果有帮助,当我在程序中调用它时,我要做的就是

If it helps, when I call this into my program, all I do is

FileManager fm = new FileManager();
JScrollPane fmsp = new JScrollPane(fm.getGUI);

推荐答案

一切正常,除了目录句柄.我不知道如何在程序启动后立即显示它们.

everything works fine, except for the handles for the directories. I can't figure out how to get them to display as soon as the program starts.

有两种方法可以证明自己:

Two approaches suggest themselves:

  • 调用 scrollPathToVisible() ;假设DefaultMutableTreeNode,您可以使用depthFirstEnumeration()获得一个合适的TreePath,如此处所示.

  • Invoke scrollPathToVisible(); assuming DefaultMutableTreeNode, you can obtain a suitable TreePath as shown here using depthFirstEnumeration().

调用 expandRow() 对于您要扩展的每个节点:

Invoke expandRow() for each node you want expanded:

for (int i = 0; i < tree.getRowCount(); i++) {
    tree.expandRow(i);
}

这篇关于JTree首次加载时未显示句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆