JList com icone?

Salve galera…

Seguinte, eu tenho um JList e quero colocar um icone na frente de uma descricao, estou dando uma pesquisada e dizem ser necessario criar um ListCellRenderer.

estou tentando assim.

//aqui meu model
public class AnexosListModel extends DefaultListModel{
    /**
     * ListModel para Arquivos de Anexos
     */
    private ArrayList<File> lista = new ArrayList<File>();
    
    public int getSize(){
        return lista.size();
    }
    
    public Object getElementAt(int index) {        	
        return lista.get(index).getName();
    }
    
    
    public void addElement(File file) {
	int index = lista.size();
	lista.add(file);
	fireIntervalAdded(this, index, index);
    }
    
    public void removeElementAt(int index) {
	lista.remove(index);        
	fireIntervalRemoved(this, index, index);
    }    
}



//aqui como pego o arquivo de imagem para anexo
JFileChooser arquivo = new JFileChooser();  
       arquivo.setDialogTitle("Selecione a Imagem");  
       arquivo.setFileSelectionMode(JFileChooser.FILES_ONLY);  
       FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG Images", new String[]{"jpg","png"});  
       arquivo.setFileFilter(filter);        
       arquivo.setAcceptAllFileFilterUsed(false);
       arquivo.setMultiSelectionEnabled(false);  
       File imageFile = new File("user.dir");  
       int option = arquivo.showOpenDialog(this);  
       
       if(option == JFileChooser.APPROVE_OPTION) {
               imageFile = arquivo.getSelectedFile();
                              
               if(imageFile.getName().endsWith(".jpg") || imageFile.getName().endsWith(".png")){                     
                   anexoModel.addElement(imageFile);                   
                   listaAnexo.setModel(anexoModel);
                   listaAnexo.setCellRenderer(new AnexosCellRenderer());
               }else{
                   JOptionPane.showMessageDialog(null, "Arquivo não suportado", "Erro", JOptionPane.ERROR_MESSAGE);                                      
               }
               
       } 


//aqui o Renderer
public class AnexosCellRenderer implements ListCellRenderer{
    
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        JLabel label = new JLabel();
        label.setIcon(new ImageIcon(getClass().getResource("/imagens/photo.png")));        
        return label;
    }    
}

Como eu faco pra colocar o icone e o nome do arquivo no JList ?

obrigado

Você deve fazer o cast do Object que você recebe (value) para um File e usar suas propriedades.

[code]

//aqui o Renderer
public class AnexosCellRenderer implements ListCellRenderer{

@Override  
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {  
    File anexo = (File)value;
    JLabel label = new JLabel();  
    label.setIcon(new ImageIcon(getClass().getResource("/imagens/photo.png")));          
    label.setText(file.getSimpleName());
    return label;  
}      

} [/code]

[quote=ViniGodoy]Você deve fazer o cast do Object que você recebe (value) para um File e usar suas propriedades.

[code]

//aqui o Renderer
public class AnexosCellRenderer implements ListCellRenderer{

@Override  
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {  
    File anexo = (File)value;
    JLabel label = new JLabel();  
    label.setIcon(new ImageIcon(getClass().getResource("/imagens/photo.png")));          
    label.setText(file.getSimpleName());
    return label;  
}      

} [/code][/quote]

Perfeito Vini, 100% funcional…obrigado.