JFileChooser fileChooser = new JFileChooser ();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.showOpenDialog();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.showOpenDialog();
解决方案 »
- ObjectInputStream如果从txt文件中读取2个java类?ObjectInputStream的实例也没有next方法呀?如何判断……
- [求助]有关JMenuItem的热键问题
- 我写了个当前月的日历,可是总天数不对?高手们能指出哪里错了吗?
- 哪位大哥能帮在下写一个完整的JDBC数据库操作类?过年了,帮帮忙,我实在是没米了。。。
- 请各位Java高手出手!!!小弟跪求答案!(急,急)
- 这样用replaceAll方法为什么不对
- 怎样执行我的程序?
- 如何用Java Applet实现柱状统计图?
- 请问:初学Java应该如何下手
- JAVA安卓手机连接服务器,WIFI可连正常使用,移动网络连不上
- 我的声音程序,为什么 不能播放啊
- JBuilder问题!!!
import java.awt.*;
public class FileChooserTest extends JFrame {
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
JFileChooser jFileChooser1 = new JFileChooser(); public FileChooserTest() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jPanel1.setLayout(borderLayout2);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jFileChooser1, BorderLayout.CENTER);
}
}
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html修改后程序如下:import javax.swing.*;
import java.awt.*;
public class FileChooserTest extends JFrame {
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
JFileChooser jFileChooser1 = new JFileChooser(); public FileChooserTest() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jPanel1.setLayout(borderLayout2);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jFileChooser1, BorderLayout.CENTER);
this.setSize(400,300) ;
this.setVisible(true);
} public static void main(String args[]){
new FileChooserTest();
}
}
如果要自己写,用JFileChooser没错,用它得到目录结构,然后对他扩展,最好是一层一层扩展,以后点选那条就扩展那条(不要用递归的一次扩展完),用JTree显示出来,具体做法可以自己研究一下.
估计对你有用,就给你吧!
//package myswing;
import javax.swing.tree.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;public class FileTreeNode extends DefaultMutableTreeNode
{
private DefaultMutableTreeNode scanAndAddNode(File filename)
{ if(filename ==null)return null;
DefaultMutableTreeNode parent = new DefaultMutableTreeNode(filename.getName());
if(filename.isDirectory())
{
parent.setAllowsChildren(true);
File[] subfiles = filename.listFiles();
for(int i = 0;i<subfiles.length;i++)
parent.add(scanAndAddNode(subfiles[i]));
}
return parent;
}
public FileTreeNode(File filename)
{
super(filename.getName());
File[] subfiles = filename.listFiles();
for(int i = 0;i<subfiles.length;i++)
{
add(scanAndAddNode(subfiles[i]));
}
}
}
测试时,加入下面这些代码
public static void main(String[] args){
JFrame f =new JFrame();
File music = new File("j:\\music");
JTree tree = new JTree(new FileTreeNode(music));
JScrollPane sp = new JScrollPane(tree);
f.getContentPane().add(sp);
f.show();
}
缺点:程序启动时较慢,因为要实现遍历,可以考虑要程序运行是动态的添加文件节点,即你打开一个文件夹节点时,添加它下面的节点,一个文件夹下应该没有多少文件,这样做可以比较快,你自己改吧
/**
* Constructs a <code>JFileChooser</code> using the given current directory
* and <code>FileSystemView</code>.
*/
public JFileChooser(File currentDirectory, FileSystemView fsv) {
setup(fsv);
setCurrentDirectory(currentDirectory);
}
/**
* Performs common constructor initialization and setup.
*/
protected void setup(FileSystemView view) {
if(view == null) {
view = FileSystemView.getFileSystemView();
}
setFileSystemView(view);
updateUI();
if(isAcceptAllFileFilterUsed()) {
setFileFilter(getAcceptAllFileFilter());
}
}
肯定可以构造出你想要的界面来