我找了段Jtree的代码,能够显示我指定目录下的文件,我还增加了确定和取消的按纽,我是想在按确定的时候,能够把我在上面选择的文件的名字保存下来,怎么能够实现啊,确定的监听我知道,主要是怎么能保存文件名,谢谢大家了,代码如下:
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.EventObject;
public class Gongxiangmulu extends JFrame
{
JButton queding2 = new JButton( "下载 ");
JButton quxiao2 = new JButton( "取消 ");
Box box1=Box.createHorizontalBox();
String mulu;
public Gongxiangmulu() {
super( "共享目录 ");
setBounds(350,200,400,500);
setVisible(true);
final JTree tree = new JTree(createTreeModel());
JScrollPane l_scrollPane = new JScrollPane(tree);
getContentPane().add(l_scrollPane);
box1.add(Box.createHorizontalStrut(120));
box1.add(queding2);
box1.add(Box.createHorizontalStrut(10));
box1.add(quxiao2);
getContentPane().add(box1, BorderLayout.SOUTH);
//getContentPane().add(GJApp.getStatusArea(), BorderLayout.SOUTH);
tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {
UpdateStatus updateThread;
TreePath path = e.getPath();
FileNode1 node = (FileNode1)
path.getLastPathComponent();
if( ! node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
UpdateStatus us = new UpdateStatus();
us.start();
node.explore();
model.nodeStructureChanged(node);
}
}
class UpdateStatus extends Thread {
public void run() {
try { Thread.currentThread().sleep(450); }
catch(InterruptedException e) { }
}
}
});
}
DefaultTreeModel createTreeModel() {