Newline in JLabel
Solution 1
Surround the string with <html></html>
and break the lines with <br/>
.
JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
Solution 2
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!
Solution 3
You can use the MultilineLabel component in the Jide Open Source Components.
http://www.jidesoft.com/products/oss.htm
Solution 4
You can do
JLabel l = new JLabel("<html><p>Hello World! blah blah blah</p></html>", SwingConstants.CENTER);
and it will automatically wrap it where appropriate.
Solution 5
Thanks Aakash for recommending JIDE MultilineLabel. JIDE's StyledLabel is also enhanced recently to support multiple line. I would recommend it over the MultilineLabel as it has many other great features. You can check out an article on StyledLabel below. It is still free and open source.
Related videos on Youtube
mportiz08
Updated on July 08, 2022Comments
-
mportiz08 almost 2 years
How can I display a newline in
JLabel
?For example, if I wanted:
Hello World!
blahblahblahThis is what I have right now:
JLabel l = new JLabel("Hello World!\nblahblahblah", SwingConstants.CENTER);
This is what is displayed:
Hello World!blahblahblah
Forgive me if this is a dumb question, I'm just learning some Swing basics...
-
Simon Baars about 7 yearsDefinitely not a dumb question. Took me a while to figure out html was possible in swing too.
-
-
AnnanFay about 13 yearssigh I tried to add backticks to escape your html, however it says I can't edit unless I'm adding at least 6 characters.
-
Nitin Bansal about 12 yearsjust a little correction: use <br /> instead of just <br> ... this is recommended way of doing it (to not miss any closing tags)...happy coding...
-
gsgx about 12 years@NitinBansal Actually it's recommended in the new version of HTML to leave it as <br>. It's called a void tag. <br /> still works for backwards compatibility.
-
Nitin Bansal about 12 years@gsingh2011 : ok...thats better :-)
-
arkon almost 11 years@Annan That isn't necessary in HTML. What you're talking about is for supporting XHTML. stackoverflow.com/questions/1946426/html-5-is-it-br-br-or-br
-
AnnanFay almost 11 years@b1naryatr0phy na, the problem was fixed :) The original post had a literal
<br>
tag which was formatted by stack-overflow as a literal line break in the post. -
ApproachingDarknessFish almost 11 yearsAny idea WHY JLabel's don't support newlines by default?
-
Roberto over 8 yearsYou actually don't even have to close the html tag. If you need to append text on runtime, this simplifies it a lot!