Results 1 to 4 of 4
  1. #1
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default How to create tree view of the given JList implementation

    Hi experts,
    I have created the desktop like appearance with a JList, with the attached code.
    How can I implement the tree-view of the same, using the methods I adopted for getting the System icons?
    In short I need a Jtree version of the attached code.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.lang.reflect.Method;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.ListCellRenderer;
    import javax.swing.UIManager;
    import javax.swing.filechooser.FileSystemView;
     
    import sun.awt.shell.ShellFolder;
     
    public class MainClass {
      public static void main(String args[]) throws Exception {
             
        MainClass mc = new MainClass();
             Object elements[][] = { {"first.doc",mc.getIcone(mc.getExtension("first.doc".toUpperCase()))},
                                                              {"second.pdf",mc.getIcone(mc.getExtension("second.pdf".toUpperCase()))},      
                                                              {"third.txt",mc.getIcone(mc.getExtension("third.txt".toUpperCase()))} ,
                                                              {"first.doc",mc.getIcone(mc.getExtension("first.doc".toUpperCase()))},
                                                              {"second.pdf",mc.getIcone(mc.getExtension("second.pdf".toUpperCase()))},      
                                                              {"third.txt",mc.getIcone(mc.getExtension("third.txt".toUpperCase()))} 
              
              
              };
              
     
        JFrame frame = new JFrame("Trial");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        JList jlist = new JList(elements);
        ListCellRenderer renderer = new ComplexCellRenderer1();
        jlist.setCellRenderer(renderer);
        jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        jlist.setVisibleRowCount(-1);
        JScrollPane scrollPane = new JScrollPane(jlist);
        frame.add(scrollPane, BorderLayout.CENTER);
     
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
      
      public String getExtension(String name)
            {
                    if(name.lastIndexOf(".")!=-1)
                    {
                            String extensionPossible = name.substring(name.lastIndexOf(".")+1, name.length());
                            if(extensionPossible.length()>6)
                            {
                                    return "";
                            }
                            else 
                            {
                                    return extensionPossible;
                            }
                    }
                    else return "";
            }
            public Icon getIcone(String extension)
            {
                    File file;
                    String cheminIcone = "";
                    if(((System.getProperties().get("os.name").toString()).startsWith("Mac")))
                            cheminIcone = System.getProperties().getProperty("file.separator");
                    else if(((System.getProperties().get("os.name").toString()).startsWith("Linux")))
                            cheminIcone = "/"+"tmp"+"/BoooDrive-"+System.getProperty("user.name")+"/";
                    else cheminIcone = System.getenv("TEMP") + System.getProperties().getProperty("file.separator");
                    
                    File repIcone = new File(cheminIcone);
                    if(!repIcone.exists()) repIcone.mkdirs();
                    
                    try
                    {
                            if(extension.equals("FOLDER"))
                            {
                                    file = new File(cheminIcone + "icon");
                                    file.mkdir();
                            }
                            else
                            {
                                    file = new File(cheminIcone + "icon." + extension.toLowerCase());
                                    file.createNewFile();
                            }
                            
                            Icon icone = getSystemIcon(file);
                            
                            file.delete();
                            return icone;
                    } 
                    catch (IOException e){ }
                    return null;
            }
            public static Icon getSystemIcon(File f) {
                if (f != null) {
                    Class fsv = FileSystemView.class;
                    try {
                        Method m = fsv.getDeclaredMethod("getShellFolder", new Class[]{File.class});
                        m.setAccessible(true);
                        ShellFolder sf = (ShellFolder) m.invoke(FileSystemView.getFileSystemView(), f);
                        Image img = sf.getIcon(true);
                        if (img != null) {
                            return new ImageIcon(img, sf.getFolderType());
                        } else {
                            return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
                        }
                    } catch (Exception e) {e.printStackTrace();}
                } 
                return null;
            }
     
    }
     
    class ComplexCellRenderer1 implements ListCellRenderer {
      protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
     
      public Component getListCellRendererComponent(JList list, Object value, int index,
          boolean isSelected, boolean cellHasFocus) {
        Icon icon = null;
        String theText = null;
     
        JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
            isSelected, cellHasFocus);
        renderer.setVerticalTextPosition(JLabel.BOTTOM);
        renderer.setHorizontalTextPosition(JLabel.CENTER);
     
     
        if (value instanceof Object[]) {
          Object values[] = (Object[]) value;
          theText = (String) values[0];
          icon = (Icon) values[1];
        } else {
          
        }
        renderer.setText(theText);
        renderer.setIcon(icon);
        return renderer;
      }
    }
    Any help is greatly appreciated.
    Regards,
    Anees

  2. #2
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    Please provide some help.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I need a Jtree version of the attached code.
    Fastest way would be to start coding it and come back with your problems.
    Do you have any specific questions?

  4. #4
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    Thanks Norm.
    Please give me some tips as to how should I implement this. I am asking for the best method that I can adapt while coding. I referred sun's tutorial for JTree, but did not get much help from there(relative to my specific need- Jtree with icons).
    Can you please say the steps that I should begin with such that I can develop a Jtree with system icons as I did with JList.
    I need only some hints,tips or links, just a guidance.
    Anees

Similar Threads

  1. How to create a SWT Tree
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:54 PM
  2. How to create a SWT Tree with multi columns
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM
  3. Binary Tree Implementation in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:35 PM
  4. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM
  5. Create view of files in java-Swing
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2007, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •