views:

264

answers:

2

I have this code in a ListCellRenderer which extends JEditorPane. The editor pane doesn't show the image, but instead shows a 'broken icon'. What's wrong with it?

public class TweetCellRenderer extends JEditorPane implements ListCellRenderer {

    public Component getListCellRendererComponent(
        javax.swing.JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus
    ) {

        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 6));

        StringBuffer sb = new StringBuffer();

        setContentType("text/html");

        sb.append("<html><body>");
        sb.append("<img src='http://www.google.co.uk/images/firefox/video.png' />");

        sb.append("</body></html>");

        System.out.println(sb);

        setText(sb.toString());  

        setBackground(isSelected ? SELECTED_BG : BG);        
        setForeground(isSelected ? SELECTED_FG : FG);

        return this;
    }
}
A: 

Every time the cell is rendered, the HTML gets parsed again. Using HTML in renderers often causes poor performance, not surprisingly. If the HTML has just been parsed, I guess there hasn't been time to start the incremental loading of images. Usually the HTML would be added to the component and then there would be a delay whilst the repaint event comes around, which allows image loading in a separate thread to do its stuff.

You can probably get away with returning a component associated with each cell.

Tom Hawtin - tackline
A: 

Create an ImageIcon from the URL and then add the icon to the list model. I think JList has a defautl renderer for icons.

camickr
I've tried using an ImageIcon. It was working when my CellRenderer was a JLabel with an attached icon, and text. But I need to have hyperlinks in the text, so I need to use an JEditorPane.I've tried extending JPanel and adding a ImageIcon (inside JLabel) and JEditorPane to the JPanel but this doesn't work either - the pictures/text is duplicated many times on top of each other for some reason.I want each item in the JList to display a 48x48 picture on the left and on the right to formatted text with hyperlinks. How can I do this?Thanks
James Hamilton