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 -
import java.awt.BorderLayout;
-
import java.awt.Component;
-
import java.awt.Dimension;
-
import java.io.File;
-
import java.util.*;
-
-
import javax.swing.*;
-
import javax.swing.border.EmptyBorder;
-
import javax.swing.filechooser.FileSystemView;
-
import javax.swing.tree.DefaultTreeCellRenderer;
-
import javax.swing.tree.TreeNode;
-
-
-
public class FileTreePanel extends JPanel {
-
-
protected static FileSystemView fsv = FileSystemView.getFileSystemView();
-
-
-
private static class FileTreeCellRenderer extends DefaultTreeCellRenderer {
-
-
private Map<String, Icon> iconCache = new HashMap<String, Icon>();
-
private Map<File, String> rootNameCache = new HashMap<File, String>();
-
-
public Component getTreeCellRendererComponent(JTree tree, Object value,
-
boolean sel, boolean expanded, boolean leaf, int row,
-
boolean hasFocus) {
-
FileTreeNode ftn = (FileTreeNode) value;
-
File file = ftn.file;
-
String filename = "";
-
if (file != null) {
-
if (ftn.isFileSystemRoot) {
-
filename = this.rootNameCache.get(file);
-
if (filename == null) {
-
filename = fsv.getSystemDisplayName(file);
-
this.rootNameCache.put(file, filename);
-
}
-
} else {
-
filename = file.getName();
-
}
-
}
-
JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
-
filename, sel, expanded, leaf, row, hasFocus);
-
if (file != null) {
-
Icon icon = this.iconCache.get(filename);
-
if (icon == null) {
-
// System.out.println("Getting icon of " + filename);
-
icon = fsv.getSystemIcon(file);
-
this.iconCache.put(filename, icon);
-
}
-
result.setIcon(icon);
-
}
-
return result;
-
}
-
}
-
-
-
private static class FileTreeNode implements TreeNode {
-
-
private File file;
-
private File[] children;
-
private TreeNode parent;
-
private boolean isFileSystemRoot;
-
-
public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
-
this.file = file;
-
this.isFileSystemRoot = isFileSystemRoot;
-
this.parent = parent;
-
this.children = this.file.listFiles();
-
if (this.children == null)
-
this.children = new File[0];
-
}
-
public FileTreeNode(File[] children) {
-
this.file = null;
-
this.parent = null;
-
this.children = children;
-
}
-
public Enumeration<?> children() {
-
final int elementCount = this.children.length;
-
return new Enumeration<File>() {
-
int count = 0;
-
-
-
public boolean hasMoreElements() {
-
return this.count < elementCount;
-
}
-
-
-
public File nextElement() {
-
if (this.count < elementCount) {
-
return FileTreeNode.this.children[this.count++];
-
}
-
throw new NoSuchElementException("Vector Enumeration");
-
}
-
};
-
-
}
-
public boolean getAllowsChildren() {
-
return true;
-
}
-
public TreeNode getChildAt(int childIndex) {
-
return new FileTreeNode(this.children[childIndex],
-
this.parent == null, this);
-
}
-
public int getChildCount() {
-
return this.children.length;
-
}
-
public int getIndex(TreeNode node) {
-
FileTreeNode ftn = (FileTreeNode) node;
-
for (int i = 0; i < this.children.length; i++) {
-
if (ftn.file.equals(this.children[i]))
-
return i;
-
}
-
return -1;
-
}
-
public TreeNode getParent() {
-
return this.parent;
-
}
-
public boolean isLeaf() {
-
return (this.getChildCount() == 0);
-
}
-
}
-
-
private JTree tree;
-
public FileTreePanel() {
-
this.setLayout(new BorderLayout());
-
-
File[] roots = File.listRoots();
-
FileTreeNode rootTreeNode = new FileTreeNode(roots);
-
this.tree = new JTree(rootTreeNode);
-
this.tree.setCellRenderer(new FileTreeCellRenderer());
-
this.tree.setRootVisible(false);
-
final JScrollPane jsp = new JScrollPane(this.tree);
-
jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
-
this.add(jsp, BorderLayout.CENTER);
-
-
}
-
-
}
-
-
2 1691
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |