Is there any MultiLine JLabel exist?

Jame picture Jame · Sep 16, 2011 · Viewed 15.3k times · Source

I want to display some text in JLabel at runtime. I just want to know that is there anyway through which the text is dispalyed on multiple line e.g i want my text to be displayed in following format:

Line 1
Line 2
Line 3

String  sText  = "Line1 \n Line2 \n Line3";
jLabel1.setText (sText);

I tried the above code but its not working. Am i doning some thing wrong or JLabel doesn't support above said feature?

In case if i am unable to achieve the above functionality then please guide me how do i add multiple labels (one for each line) in JPanel at runtime

Answer

Chandra Patni picture Chandra Patni · Sep 16, 2011

JLabel supports HTML. You can write:

String  sText  = "<html>Line1 <br/> Line2 <br/> Line3</html>";
jLabel1.setText (sText);

Edit: I added back slashes with br tag in order to make code working