I have a JLabel
I'm populating it like this
String descriptionText = StringEscapeUtils.escapeHtml(rule.getDescription());
descriptionText = descriptionText.replaceAll("\n", "<br>");
descriptionLbl.setText(String.format("<html>%s</html>", descriptionText));
In this case, if rule.getDescription()
returns something like "/* test */"
the end result is an empty JLabel
? Why is this? I thought that StringEscapeUtils#escapeHtml
would have caught all the escape characters.
Here's a smaller test app that shows the problem:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LabelTest {
public static void main(String args[]) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel();
String text = "<html>/test</html>";
label.setText(text);
frame.add(label);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
The only fix I've found is to use the special html code instead:
String text = "<html>/test</html>";
You could have a processing method that uses replaceAll() to automate this process.
I'm not sure why this happens, and it doesn't seem to happen with any other special character. Weird.