1

I made a jLabel with a Text that is to big to fit in one line, so I wanted to extend the string in the Label to multiple Lines using \n, but it did not work:

jLabel1.setBounds(8, 8, 267, 49);
    jLabel1.setText("Gib die Zeit,\n in der der Computer heruntergefahren werden soll in Sekunden, Minuten und Stunden an und drücke dann auf Herunterfahren");
    cp.add(jLabel1);

The text displayed on the Label is "Gib die Zeit,\n in der der Computer heruntergefa...". Why doesn't \n work?

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Gilgamesch
  • 293
  • 2
  • 6
  • 22
  • Use , for [example](http://stackoverflow.com/questions/29550524/jlabel-with-multiple-lines-and-alignment-to-the-right/29551195#29551195) and [example](http://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) or [example](http://stackoverflow.com/questions/19968550/beginner-java-netbeans-how-do-i-display-for-loop-in-jlabel/19968713#19968713) – MadProgrammer May 03 '15 at 11:14

1 Answers1

2

Wrap it in HTML Tags and use <br> instead of \n:

jLabel1.setText("<html>Gib die Zeit,<br> in der der Computer heruntergefahren werden soll in Sekunden, Minuten und Stunden an und drücke dann auf Herunterfahren</html>");
Spycrab0
  • 131
  • 2
  • 1
    Inserting `
    ` characters is at best a hack. Use styles to allow the HTML rendering engine to determine where line breaks should be. E.G. as in [this example](http://stackoverflow.com/a/7861833/418556).
    – Andrew Thompson May 03 '15 at 12:45