Thread: JList issues
View Single Post
  #1 (permalink)  
Old 09-04-2008, 03:10 PM
aneesahamedaa aneesahamedaa is offline
Member
 
Join Date: Jul 2008
Posts: 26
Rep Power: 0
aneesahamedaa is on a distinguished road
Default JList issues
Hi all,
In the attached code, I need to have the following options
1) get the name(eg;first.doc) of the selected item/items.
2) When I right click on top of a particular item, I need to have that item alone selected.
3) I need to have no selection for elements once I click outside the list.
I am attaching the code I developed, here.
Kindly help implementing these.
Any help is greatly appreciated.
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);
    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 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);
    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;
  }
}
Reply With Quote