【问题标题】:How to display computer files and folders in java swing GUI [duplicate]如何在java swing GUI中显示计算机文件和文件夹[重复]
【发布时间】:2011-10-10 14:23:52
【问题描述】:

可能重复:
Swing JTree with Checkbox and JFileChooser

我正在用java swings开发一个应用程序。我用jfilechooser打开了我电脑中的文件和目录。但是jfilechooser中的目录和文件的结构不是我想要的。我想要这样

我怎样才能做到这一点?任何建议都会非常有帮助。在此先感谢。

【问题讨论】:

  • 检查第一个答案,codereview.stackexchange.com/questions/4446/file-browser-gui 祝你好运!
  • @Brogrammer 哎呀,我会这么说的!我想我只是补充一点,虽然链接的示例使用JFileChooser,但使用 Puce 建议的FileSystemView 将获得 any PLAF 的最佳图标,而文件选择器需要 native PLAF 以生成漂亮的图标。
  • 顺便说一句 - 复选框是必需的吗? JTree 默认通过更改 BG 颜色来显示选择,但可以使用渲染器进行更改。
  • @Andrew Thompson 是的,但是我很惊讶看到这个问题仍然悬而未决!大声笑那里至少有 6 到 7 个重复项,并且没有一个标记为已关闭 :( 版主似乎喜欢这个 JTree 大声笑

标签: java swing jfilechooser


【解决方案1】:

要显示正确的图标,我建议使用 javax.swing.filechooser.FileSystemView

以下是我编写的一些类,用于创建文件系统根列表和组合框:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFileRootList.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFileRootComboBox.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/filechooser/FileRootComboBoxModel.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/filechooser/FileRootCellRenderer.html

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/CellRenderer.html

要使用该库,您可以使用以下 Maven 依赖项:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>lib-core</artifactId>  
    <version>0.1</version>  
</dependency>  

或者从这里下载: http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1/

这里是来源:

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/main/java/org/softsmithy/lib/swing/JFileRootList.java

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/main/java/org/softsmithy/lib/swing/JFileRootComboBox.java

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/main/java/org/softsmithy/lib/swing/filechooser/FileRootComboBoxModel.java

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/main/java/org/softsmithy/lib/swing/filechooser/FileRootCellRenderer.java

http://softsmithy.hg.sourceforge.net/hgweb/softsmithy/lib/main-golden/file/5c4db802573b/lib-core/src/main/java/org/softsmithy/lib/swing/CellRenderer.java

【讨论】:

  • +1 我没有费心去研究过多的链接(这些链接可能值得他们自己投票),但FileSystemView 是获得漂亮图标的好方法。原因请参阅我对 Brogrammer 的评论。
【解决方案2】:

TreeCellRenderer你需要使用JTree。您可以构建自己的节点数据结构,按照您认为合适的方式组织它们,然后使用 TreeCellRenderer 渲染它们。

【讨论】:

  • 好的编辑建议安德鲁。我在早上喝咖啡之前发布了原始答案。 :-)
猜你喜欢
  • 1970-01-01
  • 2011-03-22
  • 2017-12-25
  • 2012-10-14
  • 2020-02-03
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多