在ListCellRenderer JAVA中访问自定义对象

时间:2015-06-04 10:35:32

标签: java jlist renderer

我有自定义对象,其中包含特定的图片网址&名称。我有ListCellRenderer类来显示图像和文本。我想问一下如何在ListCellRenderer类中访问自定义对象的图像URL? 我的ListCellRenderer类:

class CustomCellRenderer extends JLabel implements  ListCellRenderer    
{
 private    ImageIcon   image;

public CustomCellRenderer()
{
    setOpaque(true);

    // I want to use list item's image url to get image here
    image = new ImageIcon( "img/mark.png" );
}

public Component getListCellRendererComponent(
        JList list, Object value, int index, 
        boolean isSelected, boolean cellHasFocus ) 
{
    // Display the text for this item
    setText(value.toString());
    // Set the correct image
    setIcon( image );
    // Draw the correct colors and font
    //      if( isSelected )
    //      {
    //          // Set the color and font for a selected item
    //          setBackground( Color.red );
    //          setForeground( Color.white );
    //      }
    //      else
    //      {
    //          // Set the color and font for an unselected item
    //          setBackground( Color.white );
    //          setForeground( Color.black );
    //      }

    return this;
}
}

0 个答案:

没有答案