Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

TreeCellRenderer

Thread view: 
atblock@gmail.com - 20 Jul 2006 13:32 GMT
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
atblock@gmail.com - 20 Jul 2006 20:52 GMT
hello? please help!!
Roland de Ruiter - 21 Jul 2006 14:08 GMT
> 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
[quoted text clipped - 36 lines]
>
> 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[i];
                        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

Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.