【问题标题】:Modification to JFileChooser to just display folder content修改 JFileChooser 以仅显示文件夹内容
【发布时间】:2012-03-09 14:45:49
【问题描述】:

我正在尝试开发一个 gui 应用程序,它在左侧显示文件系统树,在右侧显示选定的树节点(文件夹)的内容。 任何人都可以告诉我在 jfilechooser 中进行修改以显示文件夹内容吗 提前谢谢你

【问题讨论】:

  • 在其他论坛交叉发布。

标签: java swing directory jfilechooser


【解决方案1】:

JFileChooser#accept 允许您过滤显示哪些文件。类似的方法是JFileChooser#setFileFilter 方法

【讨论】:

  • 我想我在问问题时很困惑我只想显示文件夹内容而不想选择任何文件或文件夹只想显示当前在 jpanel 左侧的 jtree 中选择的文件夹内容,所以我要求不要进行任何修改,以便只能显示 jfilechooser 的内容显示区域,而不是所有按钮,如保存打开和右侧的快捷方式,我希望我很清楚,谢谢您的支持
【解决方案2】:

见:example

JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

【讨论】:

  • 我想我在问问题时很困惑我只想显示文件夹内容而不想选择任何文件或文件夹只想显示当前在 jpanel 左侧的 jtree 中选择的文件夹内容,所以我要求不要进行任何修改,以便只能显示 jfilechooser 的内容显示区域,而不是所有按钮,如保存打开和右侧的快捷方式,我希望我很清楚,谢谢您的支持
【解决方案3】:

也许这是你所期望的:-

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多