How can I make the text of a JLabel extend onto another line?
-
1@ChrisDennett - It's also ok to just close as a dup without any smart remarks :) – ripper234 Feb 07 '12 at 12:46
-
I think it was before I even had the privilege of voting to close. – Chris Dennett Feb 07 '12 at 13:53
11 Answers
You can do it by putting HTML in the code, so:
JFrame frame = new JFrame();
frame.setLayout(new GridLayout());
JLabel label = new JLabel("<html>First line<br>Second line</html>");
frame.add(label);
frame.pack();
frame.setVisible(true);

- 20,798
- 10
- 58
- 67
-
I have two panel..how can I make the second panel appear to the next line ? e.g. lbl1 -- first line, lbl2 --- second line. JLabel lbl1 = new JLabel("Label 1:"); panel.add(lbl1); JLabel lbl2 = new JLabel("Label 2"); panel.add(lbl2); – Jessy Mar 26 '09 at 12:56
-
1You could get some formatting issues if the default text style for labels does not match that of html. Although nine times out of ten it will work... – Tom Hawtin - tackline Mar 26 '09 at 13:02
-
1For 2 panels below each other just use a GridLayout with 2 rows and 1 column. Easy peasy :) – tddmonkey Mar 26 '09 at 14:28
-
7This is me predicting the future on StackOverflow, two years from now: "How can I make the letter `A` in my JLabel?" Answer: Make the JLabel use HTML and use the HTML Entity `A` – Simon Forsberg Jan 09 '13 at 14:59
-
Remember to set the preferred size of the label for wrapping to take effect. – treecoder May 27 '14 at 13:34
-
@Tom Hawtin - tackline which is the best way to avoid that 1/10 th chance of getting that formatting issue. – RBz Feb 22 '16 at 07:07
-
if you want your jLabel Text to resize automaticly for example in a stretchable gridbaglayout its enough just to put its text in html tags like so:
JLabel label = new JLabel("<html>First line and maybe second line</html>");

- 1,864
- 3
- 20
- 32
I have used JTextArea for multiline JLabels.
JTextArea textarea = new JTextArea ("1\n2\n3\n"+"4\n");
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html

- 159
- 1
- 3
-
2Can't believe this is mentioned more! Everywhere else people say to use the HTML mode, which breaks a lot of things like extra spacing. – rococo May 02 '18 at 22:08
Type the content (i.e., the "text" property field) inside a <html></html>
tag. So you can use <br>
or<P>
to insert a newline.
For example:
String labelContent = "<html>Twinkle, twinkle, little star,<BR>How I wonder what you are.<BR>Up above the world so high,<BR>Like a diamond in the sky.</html>";
It will display as follows:
Twinkle, twinkle, little star,
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.
You can also use a JXLabel from the SwingX library.
JXLabel multiline = new JXLabel("this is a \nMultiline Text");
multiline.setLineWrap(true);

- 33,181
- 16
- 123
- 191

- 257
- 1
- 4
- 10
-
5This answer looked to be the best, until I noticed that JXLabel is neither standard Java, nor did you mention which library it comes from. – jr. Mar 21 '14 at 13:25
This is horrifying. All these answers suggesting adding to the start of the label text, and there is not one word in the Java 11 (or earlier) documentation for JLabel to suggest that the text of a label is handled differently if it happens to start with <html>
. Who says that works everywhere and always will? And you can get big, big surprises wrapping arbitrary text in and handing it to an html layout engine.
I've upvoted the answer that suggests JTextArea. But I'll note that JTextArea isn't a drop-in replacement; by default it expands to fill rows, which is not how JLabel acts. I haven't come up with a solution to that yet.

- 684
- 7
- 14
-
https://docs.oracle.com/javase/tutorial/uiswing/components/html.html says this, so… I guess… but it completely shoots the layout (basically makes it equivalent to a horizontal glue in a box layout), so it doesn’t work for me… – mirabilos Feb 15 '21 at 23:50
In my case it was enough to split the text at every \n
and then create a JLabel
for every line:
JPanel panel = new JPanel(new GridLayout(0,1));
String[] lines = message.split("\n");
for (String line : lines) {
JLabel label = new JLabel(line);
panel.add(label);
}
I used above in a JOptionPane.showMessageDialog

- 901
- 15
- 35
You can use JTextArea
and remove editing capabilities to get normal read-only multiline text.
JTextArea textArea = new JTextArea("line\nline\nline");
textArea.setEditable(false);

- 19
- 1
- 2
String labelText ="<html>Name :"+name+"<br>Surname :"+surname+"<br>Gender :"+gender+"</html>";
JLabel label=new JLabel(labelText);
label.setVisible(true);
label.setBounds(10, 10,300, 100);
dialog.add(label);

- 5,770
- 5
- 53
- 62
-
While this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Apr 12 '16 at 13:02
It is possible to use (basic) CSS in the HTML.
This question was linked from Multiline JLabels - Java.

- 1
- 1

- 168,117
- 40
- 217
- 433
why you are giving complex things...you can just do it by putting "\n" instead of html tags

- 1
- 1
-
1It clearly doesn't work with JLabels, will only work with other TextComponents or JXlabels – kuranes Mar 06 '16 at 21:28