TreeCellRenderer

A

atblock

I'm trying to create a TreeCellRenderer that would display a files
system icon and system name instead of the default icons so I created a
class as follows:

class FileSystemRenderer extends DefaultTreeCellRenderer {
private FileSystemView f;
public FileSystemRenderer(FileSystemView f) {
this.f = f;
}

public Component getTreeCellRenderComponent(JTree tree, Object
value, boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, selected,
expanded, leaf, row,
hasFocus);

setIcon(this.f.getSystemIcon((File)value));
setText(this.f.getSystemDisplayName((File)value));
return this;
}
}

and I created my tree as follows:

tree = new JTree(top);
tree.setCellRenderer(new FileSystemRenderer(fsv));

and my method for creating nodes gives the a File as an object...

the problem is nothing happens...it just completely ignores the
cellrenderer...

I inserted some System.out.print calls in the
getTreeCellRendererComponent class and got nothing...

Then I tried the demo from sun TreeIconDemo2 and the same problem...

my javavm is 1.4.2_04-b05
 
R

Roland de Ruiter

I'm trying to create a TreeCellRenderer that would display a files
system icon and system name instead of the default icons so I created a
class as follows:

class FileSystemRenderer extends DefaultTreeCellRenderer {
private FileSystemView f;
public FileSystemRenderer(FileSystemView f) {
this.f = f;
}

public Component getTreeCellRenderComponent(JTree tree, Object
value, boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, selected,
expanded, leaf, row,
hasFocus);

setIcon(this.f.getSystemIcon((File)value));
setText(this.f.getSystemDisplayName((File)value));
return this;
}
}

and I created my tree as follows:

tree = new JTree(top);
tree.setCellRenderer(new FileSystemRenderer(fsv));

and my method for creating nodes gives the a File as an object...

the problem is nothing happens...it just completely ignores the
cellrenderer...

I inserted some System.out.print calls in the
getTreeCellRendererComponent class and got nothing...

Then I tried the demo from sun TreeIconDemo2 and the same problem...

my javavm is 1.4.2_04-b05
Without the full code it's hard to diagnose what the problem might be.

However, your JDK/JRE is quite old. Try to upgrade to the latest release
of JDK 1.4.2 (release 12, yours is release 04):
<http://java.sun.com/j2se/1.4.2/download.html>


The following example is a simple file explorer, which does use a
TreeCellRenderer to display the system icon and display name of a file.
You may want to have a peek at it and run it.

// begin of code (save all in a file named FileExplorer.java)
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.*;

public class FileExplorer extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileExplorer app = new FileExplorer();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
});
}
private FileSystemView fileSystemView =
FileSystemView.getFileSystemView();
private JPanel jContentPane;
private JScrollPane jScrollPane;
private JTree jTree;
public FileExplorer() {
super();
initialize();
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJScrollPane(), BorderLayout.WEST);
}
return jContentPane;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTree());
}
return jScrollPane;
}
private JTree getJTree() {
if (jTree == null) {
jTree = new JTree();
jTree.setModel(new FileSystemModel(fileSystemView));
jTree.setCellRenderer(new FileNodeRenderer(fileSystemView));
}
return jTree;
}
private void initialize() {
this.setSize(400, 300);
this.setContentPane(getJContentPane());
this.setTitle("File Explorer");
}
}
class FileNode implements TreeNode {
private final static Comparator alphaOrderDirectoriesFirst
= new Comparator() {
public int compare(File f1, File f2) {
if (f1.isDirectory()) {
if (f2.isDirectory()) {
return f1.getName().compareTo(f2.getName());
} else {
return -1;
}
} else if (f2.isDirectory()) {
return +1;
} else {
return f1.getName().compareTo(f2.getName());
}
}
public int compare(Object o1, Object o2) {
return compare((File) o1, (File) o2);
}
};
private static File getRootFile(FileSystemView fileSystemView) {
File root = null;
if (fileSystemView != null) {
File[] roots = fileSystemView.getRoots();
if (roots != null && roots.length > 0) {
root = roots[0];
}
}
if (root == null) {
root = new File(".");
}
return root;
}
private File file;
private String name;
private boolean isDirectory;
private FileNode parent;
private List children;

public FileNode(File file) {
this(null, file);
}
public FileNode(FileNode parent, File file) {
this.parent = parent;
this.file = file;
this.isDirectory = file.isDirectory();
}
public FileNode(FileSystemView fileSystemView) {
this(getRootFile(fileSystemView));
}
public Enumeration children() {
return new Enumeration() {
final Iterator i = getChildren().iterator();
public boolean hasMoreElements() {
return i.hasNext();
}
public Object nextElement() {
return i.next();
}
};
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final FileNode other = (FileNode) obj;
if (file == null) {
if (other.file != null) {
return false;
}
} else if (!file.equals(other.file)) {
return false;
}
return true;
}
public boolean getAllowsChildren() {
return true;
}
public TreeNode getChildAt(int childIndex) {
return (FileNode) getChildren().get(childIndex);
}
public int getChildCount() {
return getChildren().size();
}
private List getChildren() {
if (children == null) {
children = new ArrayList();
if (isDirectory) {
File[] files = file.listFiles();
if (files != null && files.length > 1) {
Arrays.sort(files, alphaOrderDirectoriesFirst);
}
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files;
children.add(new FileNode(this, file));
}
}
}
}
return children;
}
public File getFile() {
return file;
}
public int getIndex(TreeNode node) {
if (node != null) {
List c = getChildren();
for (int i = 0; i < c.size(); i++) {
FileNode fn = (FileNode) c.get(i);
if (fn.equals(node)) {
return i;
}
}
}
return -1;
}
private String getName() {
if (name == null && file != null) {
name = file.getName();
}
return name;
}
public TreeNode getParent() {
return parent;
}
public int hashCode() {
return 31 + ((file == null) ? 0 : file.hashCode());
}
public boolean isLeaf() {
return !isDirectory;
}
public String toString() {
return file == null ? super.toString() : getName();
}
}
class FileNodeRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = -8038247326841588275L;
private FileSystemView fileSystemView;
public FileNodeRenderer(FileSystemView fileSystemView) {
this.fileSystemView = fileSystemView;
}
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
Component rc = super.getTreeCellRendererComponent(tree, value,
sel, expanded, leaf, row, hasFocus);
if (rc instanceof JLabel && value instanceof FileNode) {
JLabel jl = (JLabel) rc;
File f = ((FileNode) value).getFile();
jl.setText(fileSystemView.getSystemDisplayName(f));
jl.setIcon(fileSystemView.getSystemIcon(f));
}
return rc;
}
}
class FileSystemModel extends DefaultTreeModel {
private static final long serialVersionUID = -5397362508488554215L;
public FileSystemModel(FileSystemView fileSystemView) {
this(new FileNode(fileSystemView));
}
public FileSystemModel(TreeNode root) {
super(root);
}
}
// end of code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

change icon in jtree 0
TreeCellRenderer in Linux 16
TreeCellRenderer does not work? 2
Jtree renderers 0
[SWING] Problem with JTree 2
checkbox ploblem pls help!! 15
ploblem in jcheckbox 0
JTable within JTree 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top