gpt4 book ai didi

java - 用包列表填充树的快速而肮脏的方法

转载 作者:行者123 更新时间:2023-12-02 07:47:13 25 4
gpt4 key购买 nike

是否有一种快速而肮脏的方法来填充指定包的 jtree(根节点和子节点)?

例如:

com.mycompany.components.ComponentA
com.mycompany.components.ComponentB
com.mycompany.components.ComponentC

有没有伪代码的方法:

Jtree tree = new JTree( Package.getPackage("com.mycompany") );

所以最终的结果是:

com.mycompany.components
|---- com.mycompany.components.ComponentA
|---- com.mycompany.components.ComponentB
|---- com.mycompany.components.ComponentC

???

最佳答案

is there a quick and dirty way to populate a jtree ( root and child nodes ) of a specified package?

是的。 Netbeans 项目结构的 Java 包:

enter image description here

import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class FileTree extends JPanel {

private static final long serialVersionUID = 1L;

public FileTree(File dir) {
setLayout(new BorderLayout());
JTree tree = new JTree(addNodes(null, dir));
tree.addTreeSelectionListener(new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
System.out.println("You selected " + node);
}
});
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
}

private DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) {
curTop.add(curDir);
}
Vector<String> ol = new Vector<String>();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++) {
ol.addElement(tmp[i]);
}
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector<Object> files = new Vector<Object>();
for (int i = 0; i < ol.size(); i++) {
String thisObject = ol.elementAt(i);
String newPath;
if (curPath.equals(".")) {
newPath = thisObject;
} else {
newPath = curPath + File.separator + thisObject;
}
if ((f = new File(newPath)).isDirectory()) {
addNodes(curDir, f);
} else {
files.addElement(thisObject);
}
}
for (int fnum = 0; fnum < files.size(); fnum++) {
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
}
return curDir;
}

@Override
public Dimension getMinimumSize() {
return new Dimension(200, 400);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}

public static void main(final String[] av) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
JFrame frame = new JFrame("FileTree");
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
Container cp = frame.getContentPane();
if (av.length == 0) {
cp.add(new FileTree(new File(".")));
} else {
cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
for (int i = 0; i < av.length; i++) {
cp.add(new FileTree(new File(av[i])));
}
}
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}

编辑

您可以删除所有JTree Icons,例如JTree with NoIcon

DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);

关于java - 用包列表填充树的快速而肮脏的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com