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