我有这个
JFileTree
:
public class JFileTree extends JPanel {
private final FileSystemView fileSystemView;
private final JTree tree;
private DefaultTreeModel treeModel;
public JFileTree() {
setLayout(new BorderLayout(3, 3));
setBorder(new EmptyBorder(5, 5, 5, 5));
fileSystemView = FileSystemView.getFileSystemView();
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
treeModel = new DefaultTreeModel(root);
TreeSelectionListener treeSelectionListener = tse -> {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tse.getPath().getLastPathComponent();
showChildren(node);
setFileDetails((File) node.getUserObject());
};
File[] roots = fileSystemView.getRoots();
for (File fsr : roots) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(fsr, true);
root.add(node);
File[] files = fileSystemView.getFiles(fsr, true);
for (File file : files) {
if (file.isDirectory()){
node.add(new DefaultMutableTreeNode(file, true));
}
}
}
tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.addTreeSelectionListener(treeSelectionListener);
tree.setCellRenderer(new FileTreeCellRenderer());
tree.expandRow(0);
showRootFile();
JScrollPane treeScroll = new JScrollPane(tree);
treeScroll.setPreferredSize(new Dimension(200, (int) treeScroll.getPreferredSize().getHeight()));
add(treeScroll, BorderLayout.CENTER);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException |
InstantiationException ignored) {}
}
public void showRootFile() {
tree.setSelectionInterval(0, 0);
}
private void showChildren(final DefaultMutableTreeNode node) {
tree.setEnabled(false);
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() {
tree.setEnabled(true);
}
};
worker.execute();
}
private void setFileDetails(File file) {
fileSystemView.getSystemIcon(file);
}
public static void main(String[] args) {
JFrame f = new JFrame("Project view");
f.setContentPane(new JFileTree());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
}
class FileTreeCellRenderer extends DefaultTreeCellRenderer {
private FileSystemView fileSystemView;
private JLabel label;
public 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.setText(fileSystemView.getSystemDisplayName(file));
label.setToolTipText(file.getPath());
boolean hasChildren = node.getChildCount() > 0;
if (hasChildren) {
if (expanded) {
label.setIcon(fileSystemView.getSystemIcon(file));
} else {
label.setIcon(fileSystemView.getSystemIcon(file.getParentFile()));
}
} else {
label.setIcon(fileSystemView.getSystemIcon(file));
}
if (selected) {
label.setBackground(backgroundSelectionColor);
label.setForeground(textSelectionColor);
} else {
label.setBackground(backgroundNonSelectionColor);
label.setForeground(textNonSelectionColor);
}
return label;
}
}
信用:@Andrew Thompson (对不起,如果它有点长,它只是 Andrew Thompson 的 FileBro 的精简版)
我的问题是下拉箭头不会出现在每个节点上,除非我单击该节点。我想要的是在应用程序初始化时显示它。
我尝试设置
tree.setShowsRootHandles(true);
,但它只适用于最顶层目录。但它没有用。我请 OpenAI 的 ChatGPT 帮我调试这个,但即使经过几个小时的调试,仍然没有任何进展(除了随机试错)。我究竟做错了什么?我不是一个非常有经验的 swing 开发人员,所以如果这是一个简单的修复,请多多包涵。这是它的样子:
这就是我想要的样子: