I would like to make an JListh which will present my html code in their cells so I do:
public class HtmlCellRenderer extends DefaultListCellRenderer{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public static final int CONST_PREFERED_HEIGHT = 200;
/**
*
*/
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
label.setPreferredSize(new Dimension( 50, CONST_PREFERED_HEIGHT ));
if (value != null) {
ExtendedJEditorPane htmlPane = new ExtendedJEditorPane();
label.setLayout(new BorderLayout());
htmlPane.setEditable(false);
htmlPane.setContentType("text/html" );
htmlPane.setText(value.toString());
label.add(htmlPane, BorderLayout.CENTER);
}
return label;
}
}
for a custom renderer. Here I make a new JEditPane ( I extend it in order to draw some custom tags in my html) and insert it to the cell’s label but the result is that my cell does not recognize the html and print the raw text instead.
The input text is:
"<table><tr><td rowspan=\"2\"><img src=\"data:image/jpeg;base64,"+myImageData+\" align=\"left\" /></td><td><h3>Test1 </h3><hr></td></tr><tr><td><p>Test</td></tr></table>"
The list entry object will return this string in case of toString execution.
Instead of:
Try using this: