package com.tarisan.chipcard.gui; import java.awt.Component; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; import java.util.*; /**** * Se encarga de personalizar un combo * @author jjrider * */ public class ComboCellRenderer extends JLabel implements ListCellRenderer { HashMap hshImagenes=new HashMap(); private int nHeight=40; public ComboCellRenderer() { super(""); setOpaque(true); setVerticalTextPosition(JLabel.CENTER); setHorizontalTextPosition(JLabel.RIGHT); setHorizontalAlignment(LEFT); setVerticalAlignment(CENTER); //setFont(new java.awt.Font("Tahoma",java.awt.Font.BOLD,14)); //setPreferredSize(new java.awt.Dimension(120,nHeight)); //setMinimumSize(new java.awt.Dimension(120,35)); } public void addIcon(Object sValue,Icon icon){ //System.out.println("addIcon:'"+sValue+"'->"+icon.getIconHeight()); hshImagenes.put(sValue.toString(),icon); if(icon!=null){ nHeight=Math.max(icon.getIconHeight(),nHeight); setPreferredSize(new java.awt.Dimension(Math.max(getWidth(),160),nHeight)); } } public void removeIcon(String sValue){ hshImagenes.remove(sValue); } public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) { if(value!=null){ setText(value.toString()); //setBackground(isSelected ? Color.GREEN : Color.white); //setForeground(isSelected ? Color.white : Color.black); } if(value!=null){ //System.out.println("getListCellRendererComponent:'"+value+"'"); Icon icon=(Icon)hshImagenes.get(value.toString()); if(icon!=null){ setIcon(icon); }else{ setIcon(null); } }else{ setText(""); setIcon(null); } return this; } }