Newline in JLabel
You can use the MultilineLabel component in the Jide Open Source Components.
http://www.jidesoft.com/products/oss.htm
Surround the string with <html></html>
and break the lines with <br/>
.
JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
You can try and do this:
myLabel.setText("<html>" + myString.replaceAll("<","<").replaceAll(">", ">").replaceAll("\n", "<br/>") + "</html>")
The advantages of doing this are:
- It replaces all newlines with
<br/>
, without fail. - It automatically replaces eventual
<
and>
with<
and>
respectively, preventing some render havoc.
What it does is:
"<html>" +
adds an openinghtml
tag at the beginning.replaceAll("<", "<").replaceAll(">", ">")
escapes<
and>
for convenience.replaceAll("\n", "<br/>")
replaces all newlines bybr
(HTML line break) tags for what you wanted- ... and
+ "</html>"
closes ourhtml
tag at the end.
P.S.: I'm very sorry to wake up such an old post, but whatever, you have a reliable snippet for your Java!