【发布时间】:2012-03-09 14:45:49
【问题描述】:
我正在尝试开发一个 gui 应用程序,它在左侧显示文件系统树,在右侧显示选定的树节点(文件夹)的内容。 任何人都可以告诉我在 jfilechooser 中进行修改以显示文件夹内容吗 提前谢谢你
【问题讨论】:
-
在其他论坛交叉发布。
标签: java swing directory jfilechooser
我正在尝试开发一个 gui 应用程序,它在左侧显示文件系统树,在右侧显示选定的树节点(文件夹)的内容。 任何人都可以告诉我在 jfilechooser 中进行修改以显示文件夹内容吗 提前谢谢你
【问题讨论】:
标签: java swing directory jfilechooser
JFileChooser#accept 允许您过滤显示哪些文件。类似的方法是JFileChooser#setFileFilter 方法
【讨论】:
见:example
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
【讨论】:
也许这是你所期望的:-
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
try {
// Create a File object containing the canonical path of the desired directory
File f = new File(new File(".").getCanonicalPath());
// Set the current directory
chooser.setCurrentDirectory(f);
} catch (IOException e1) {
e1.printStackTrace();
}
// Show the dialog; wait until dialog is closed
int returnVal = chooser.showOpenDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
textField.setText(f.getAbsolutePath());
File[] contents = f.listFiles();
for(int file=0;file<contents.length;file++)
{
System.out.println(contents[file].getName()); //here you get the contents of the selected directory
}
}
【讨论】: