For a simple JLabel, you can call the JComponent method
myLabel.putClientProperty("html.disable", Boolean.TRUE);
on the label where you want to disable HTML rendering.
Reference: Impossible to disable HTML Rendering in a JLabel
For something like a JTable, JTree, or JList you'll need to create a custom cell renderer that sets this property. Here's an example (modified from this example) that creates a custom cell renderer for a JList.
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
public class JListTest {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JList Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selections = { "<html><img src='http:\\\\invalid\\url'>",
"red", "orange", "dark blue" };
JList list = new JList(selections);
// set the list cell renderer to the custom class defined below
list.setCellRenderer(new MyCellRenderer());
list.setSelectedIndex(1);
System.out.println(list.getSelectedValue());
frame.add(new JScrollPane(list));
frame.pack();
frame.setVisible(true);
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
putClientProperty("html.disable", Boolean.TRUE);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
setText(value.toString());
return this;
}
}
I used the example code from the ListCellRenderer documentation as a starting point for the custom list cell renderer.
When I run the example, you can see that the HTML in the first list entry is rendered instead of being interpreted.