2

Possible Duplicate:
Swing JTree with Checkbox and JFileChooser

I am developing an application in java swings.I have used jfilechooser to open up the files and directories in my computer.But the structure of the directories and files in jfilechooser are not the way i wanted.I wanted it this way

enter image description here

How can i achieve this?Any suggestions would be really helpful.Thanks in advance.

Community
  • 1
  • 1
hemanth kumar
  • 3,068
  • 10
  • 41
  • 64
  • 3
    Check first answer for this, http://codereview.stackexchange.com/questions/4446/file-browser-gui Good luck! – TeaCupApp Oct 10 '11 at 14:27
  • @Brogrammer Gee I was gonna' say that! I suppose I'll just add that while the linked example uses a `JFileChooser`, using the `FileSystemView` as suggested by Puce will get the best icons for *any* PLAF, whereas the file chooser requires the *native* PLAF in order to produce the nice icons. – Andrew Thompson Oct 10 '11 at 15:37
  • BTW - are the check boxes a requirement? A `JTree` defaults to showing selection by changing the BG color, but that be changed using a renderer. – Andrew Thompson Oct 10 '11 at 15:56
  • @Andrew Thompson true, however I am amazed by seeing tht this question is still open! Lol there are atleast 6 to 7 duplicates out there and non of them marked as CLOSED :( moderator seems to love this JTree lol – TeaCupApp Oct 10 '11 at 23:31

2 Answers2

4

a TreeCellRendererYou will need to use a JTree. You can build your own data structure of nodes, organize them how you see fit, then render them with a TreeCellRenderer.

Daniel Pereira
  • 1,785
  • 12
  • 10
3

To display the correct icons I suggest to use javax.swing.filechooser.FileSystemView

Here are some classes I wrote to create a list and a combo box of file system roots:

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

To use the library you can use the following Maven dependency:

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

Or download it from here: http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1/

Here is the source:

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

Puce
  • 37,247
  • 13
  • 80
  • 152
  • +1 I did not bother to investigate the plethora of links (which are probably worth an up-vote of their own), but the `FileSystemView` is a good way to get nice icons. See my comment to Brogrammer for the reason. – Andrew Thompson Oct 10 '11 at 15:40