Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,320 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,711 people online right now. Registration is fast and FREE... Join Now!




Icons on JList

 
Reply to this topicStart new topic

Icons on JList, Adding icons to a JList

aneesahamedaa
3 Sep, 2008 - 10:05 PM
Post #1

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 1

I am 99% close to implementing the view given in //docs.google.com/Doc?id=dfzrknk_13d84m6hgg.
Still three small problems I face.
1) One is regarding the alignment. I tried different alternatives with jlist.setLayoutOrientation. But I did not get the alignment which I needed.
2) How can I bring the name under the icon?
3) How can I minimize the highlighted area during selection, so that on component selection, one element does not get touched with other element.

I am attaching the code I developed, here.
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 ComplexCellRenderer();
    jlist.setCellRenderer(renderer);
    jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP);

    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 ComplexCellRenderer 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);

    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;
  }
}


This post has been edited by aneesahamedaa: 4 Sep, 2008 - 02:04 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Icons On JList
4 Sep, 2008 - 03:20 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(aneesahamedaa @ 3 Sep, 2008 - 11:05 PM) *

I am 99% close to implementing the view given in //docs.google.com/Doc?id=dfzrknk_13d84m6hgg.
Still three small problems I face.
1) One is regarding the alignment. I tried different alternatives with jlist.setLayoutOrientation. But I did not get the alignment which I needed.
2) How can I bring the name under the icon?
3) How can I minimize the highlighted area during selection, so that on component selection, one element does not get touched with other element.


1) Which alignment do you need ?
2) Check the JLabel class setVerticalTextPosition() and apply it to your JLabel renderer in your ComplexCellRenderer
3) Don't really understand what you mean. Works perfectly for me

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:23AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month