Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-06-2008, 07:09 AM
Member
 
Join Date: Jul 2008
Posts: 26
Rep Power: 0
aneesahamedaa is on a distinguished road
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.
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-06-2008, 09:16 AM
Member
 
Join Date: Jul 2008
Posts: 26
Rep Power: 0
aneesahamedaa is on a distinguished road
Default
Please provide some help.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-06-2008, 02:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 4,790
Rep Power: 7
Norm is on a distinguished road
Default
Quote:
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-06-2008, 02:45 PM
Member
 
Join Date: Jul 2008
Posts: 26
Rep Power: 0
aneesahamedaa is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 07:58 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org