473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

JTree Help

3
Hi, I am creating an application where I want to show a file system. I have never worked with them before, so i am not sure on what I need to do to accomplish my goal. So far I found a script through google that lists all the files and folders. What I want to do is modified the script to only show just the folders, then when the user clicks on the folder, I want to populate another object (maybe JList), with only images(jpg,bmp,tiff,png,gif) that are in the folder the user clicked. Any ideas??!?!? Thanks


Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.io.File;
  5. import java.util.*;
  6.  
  7. import javax.swing.*;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.filechooser.FileSystemView;
  10. import javax.swing.tree.DefaultTreeCellRenderer;
  11. import javax.swing.tree.TreeNode;
  12.  
  13.  
  14. public class FileTreePanel extends JPanel {
  15.  
  16.     protected static FileSystemView fsv = FileSystemView.getFileSystemView();
  17.  
  18.  
  19.     private static class FileTreeCellRenderer extends DefaultTreeCellRenderer {
  20.  
  21.         private Map<String, Icon> iconCache = new HashMap<String, Icon>();
  22.         private Map<File, String> rootNameCache = new HashMap<File, String>();
  23.  
  24.         public Component getTreeCellRendererComponent(JTree tree, Object value,
  25.                 boolean sel, boolean expanded, boolean leaf, int row,
  26.                 boolean hasFocus) {
  27.             FileTreeNode ftn = (FileTreeNode) value;
  28.             File file = ftn.file;
  29.             String filename = "";
  30.             if (file != null) {
  31.                 if (ftn.isFileSystemRoot) {
  32.                     filename = this.rootNameCache.get(file);
  33.                     if (filename == null) {
  34.                         filename = fsv.getSystemDisplayName(file);
  35.                         this.rootNameCache.put(file, filename);
  36.                     }
  37.                 } else {
  38.                     filename = file.getName();
  39.                 }
  40.             }
  41.             JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
  42.                     filename, sel, expanded, leaf, row, hasFocus);
  43.             if (file != null) {
  44.                 Icon icon = this.iconCache.get(filename);
  45.                 if (icon == null) {
  46.                     // System.out.println("Getting icon of " + filename);
  47.                     icon = fsv.getSystemIcon(file);
  48.                     this.iconCache.put(filename, icon);
  49.                 }
  50.                 result.setIcon(icon);
  51.             }
  52.             return result;
  53.         }
  54.     }
  55.  
  56.  
  57.     private static class FileTreeNode implements TreeNode {
  58.  
  59.         private File file;
  60.         private File[] children;
  61.         private TreeNode parent;
  62.         private boolean isFileSystemRoot;
  63.  
  64.         public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
  65.             this.file = file;
  66.             this.isFileSystemRoot = isFileSystemRoot;
  67.             this.parent = parent;
  68.             this.children = this.file.listFiles();
  69.             if (this.children == null)
  70.                 this.children = new File[0];
  71.         }
  72.         public FileTreeNode(File[] children) {
  73.             this.file = null;
  74.             this.parent = null;
  75.             this.children = children;
  76.         }
  77.         public Enumeration<?> children() {
  78.             final int elementCount = this.children.length;
  79.             return new Enumeration<File>() {
  80.                 int count = 0;
  81.  
  82.  
  83.                 public boolean hasMoreElements() {
  84.                     return this.count < elementCount;
  85.                 }
  86.  
  87.  
  88.                 public File nextElement() {
  89.                     if (this.count < elementCount) {
  90.                         return FileTreeNode.this.children[this.count++];
  91.                     }
  92.                     throw new NoSuchElementException("Vector Enumeration");
  93.                 }
  94.             };
  95.  
  96.         }
  97.         public boolean getAllowsChildren() {
  98.             return true;
  99.         }
  100.         public TreeNode getChildAt(int childIndex) {
  101.             return new FileTreeNode(this.children[childIndex],
  102.                     this.parent == null, this);
  103.         }
  104.         public int getChildCount() {
  105.             return this.children.length;
  106.         }
  107.         public int getIndex(TreeNode node) {
  108.             FileTreeNode ftn = (FileTreeNode) node;
  109.             for (int i = 0; i < this.children.length; i++) {
  110.                 if (ftn.file.equals(this.children[i]))
  111.                     return i;
  112.             }
  113.             return -1;
  114.         }
  115.         public TreeNode getParent() {
  116.             return this.parent;
  117.         }
  118.         public boolean isLeaf() {
  119.             return (this.getChildCount() == 0);
  120.         }
  121.     }
  122.  
  123.     private JTree tree;
  124.     public FileTreePanel() {
  125.         this.setLayout(new BorderLayout());
  126.  
  127.         File[] roots = File.listRoots();
  128.         FileTreeNode rootTreeNode = new FileTreeNode(roots);
  129.         this.tree = new JTree(rootTreeNode);
  130.         this.tree.setCellRenderer(new FileTreeCellRenderer());
  131.         this.tree.setRootVisible(false);
  132.         final JScrollPane jsp = new JScrollPane(this.tree);
  133.         jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
  134.         this.add(jsp, BorderLayout.CENTER);
  135.  
  136.     }
  137.  
  138. }
  139.  
  140.  
Jan 7 '08 #1
2 1691
r035198x
13,262 8TB
Why not use a JFileChooser with an appropriate filter?
Jan 7 '08 #2
ace84
3
Why not use a JFileChooser with an appripriate filter?
thats what I am using now, I just thought it would look different and something to learn
Jan 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mitch Williams | last post by:
I've been working with a JTree based on a custom TreeModel. Unfortunatly, I cant figure out how to force it to update the entire tree-- there simply dosnt seem to be a way to say "OK, your data...
1
by: Bernard Koninckx | last post by:
Hello everybody, I'vee a small problem with a JTree. I make an expand from a node. And after this operation the selection is lost. How can I do reselect the node than I come expand ? Thanks...
2
by: Piet | last post by:
Hi there, I have managed to write a small java program that display an xml file in a JTree. This was achieved by defining a suitable tree model. THe program works...somehow. The nodes (elements...
0
by: Lucia | last post by:
hello, I habe a Java Program that displays the XML document in a Java JTree using DOM. Now I can add, delete and edit the JTree Elements. But I don't know how to show the attributes also in the...
0
by: luvping04 | last post by:
Hey, i told to make a java program to load xml into JTREE using SAX api. i'm suppose to load the 1st layer of the jtree and hide the subtree into memory. How can i load the subtree into...
0
by: flavourofbru | last post by:
Hi, I am converting a given html document into a tree structure and displaying it using JTree and DefaultMutable classes. By using the html parser and the JTree functionality, I can display the...
1
by: flavourofbru | last post by:
Hi, I am presently trying to display an XML document as a tree structure. I am able to do it using JTree and SAX Parser. Now I would like to get the parents of all non-leaf nodes and display...
1
by: sivakrish85 | last post by:
Hi, this is krishna i am on developing FTP in java.Here i display the server Files and client Files in tree view using JTree.So my question is that once the client send the file to...
5
by: uhdam | last post by:
In java, How to make a class identify its method when more than one class is present. Thats suppose i have 4 classes namely class1,class2,class3 & class4 and i have one method in each class namely...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.