Java 将JTree添加到JFileChooser
我是JTrees的新手,有一个问题: 如果我们可以在Java 将JTree添加到JFileChooser,java,swing,jtree,jfilechooser,Java,Swing,Jtree,Jfilechooser,我是JTrees的新手,有一个问题: 如果我们可以在JFileChooser中添加系统中可用文件和目录的树。基本上,我希望有一个自定义的JFileChooser,其中文件和目录可以以树的形式显示 提前感谢:) 我已经创建了一个树,其中包含我的系统的当前文件,但是如何在JFileChooser中显示该树。这是JTree的代码 public class FileTree extends JPanel { /** Construct a FileTree */ public FileTree(
JFileChooser
中添加系统中可用文件和目录的树
。基本上,我希望有一个自定义的JFileChooser,其中文件和目录可以以树的形式显示
提前感谢:)
我已经创建了一个树,其中包含我的系统的当前文件,但是如何在JFileChooser中显示该树。这是JTree的代码
public class FileTree extends JPanel {
/** Construct a FileTree */
public FileTree(File dir) {
setLayout(new BorderLayout());
// Make a tree list with all the nodes, and make it a JTree
JTree tree = new JTree(addNodes(null, dir));
// Lastly, put the JTree into a JScrollPane.
JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
add(BorderLayout.CENTER, scrollpane);
}
/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir)
{
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) { // should only be null at root
curTop.add(curDir);
}
Vector ol = new Vector();
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 files = new Vector();
// Make two passes, one for Dirs and one for Files. This is #1.
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) 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);
}
// Pass two: for files.
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}
public Dimension getMinimumSize() {
return new Dimension(200, 400);
}
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
/** Main: make a Frame, add a FileTree */
public static void main(String[] av) {
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);
}
}
公共类文件树扩展了JPanel{
/**构建一个文件树*/
公共文件树(文件目录){
setLayout(新的BorderLayout());
//制作一个包含所有节点的树列表,并使其成为JTree
JTree树=新的JTree(addNodes(null,dir));
//最后,将JTree放入JScrollPane中。
JScrollPane scrollpane=新的JScrollPane();
scrollpane.getViewport().add(树);
添加(BorderLayout.CENTER,滚动窗格);
}
/**将“dir”下的节点添加到curTop中。高度递归*/
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop,文件目录)
{
字符串curPath=dir.getPath();
DefaultMutableTreeNode curDir=新的DefaultMutableTreeNode(curPath);
if(curTop!=null){//只应在根处为null
curTop.add(curDir);
}
向量ol=新向量();
字符串[]tmp=dir.list();
for(int i=0;i
这是一个非常宽泛的问题,但这里有几个要点
如果您试图将它与jFileChooser
集成,事情可能会变得一团糟,但这是可能的
我建议您改为使用代码,但要做一些更改,使其像jFileChooser
首先:在FileTree
面板中添加一个选择/打开按钮。按下此按钮后,您可以快速获取所选文件的列表,并将其保存到共享变量中
其次:您可以使用一个共享变量将信息从FileTree
弹出窗口传递回调用它的线程(),然后您可以使用返回的文件执行预期任务,就像jFileChooser
中的返回语句一样
注:
确保只调用
FileTree
类(或者你可以阻止EDT,但这通常不是一个好主意):如果你的问题只是“这可能吗?”答案是肯定的。若你们想知道怎么做,那个么你们的问题太宽泛了,无法回答。问具体的问题。我知道这是一个广泛的问题,这就是为什么我无法理解从哪里开始,如果我需要这样一个项目@滑石粉。谢谢:)@bhavna garg您可以在FileChooser
上使用setAccesory(FileTree)
此方法将您的FileTree
添加到FileChooser的右侧
非常感谢您的想法。我想不起来了。但是如果你知道了方法,请告诉我:)