11

On a JButton, I want to list information on multiple lines. I tried \n as a new line character but it didn't work. The following code:

JButton.setText("fnord\nfoo") ;

will be displayed as:

fnordfoo

How do I force a line break?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • Am i seeing things, or did you just ask a question which you answered two seconds later? – Name Nov 21 '12 at 22:55
  • 2
    @ForgiveMeI'mAN00b I did not answer it two seconds later, I answered directly with the question itself. There is an option to directly answer your own question on the Ask-a-question-form. – k0pernikus Nov 21 '12 at 23:05
  • 2
    Stack overflow is meant to be a question-answer style forum to share knowledge and seek answers to problems you're having with your code. Asking and answering a question that you commonly face is a good way to share knowledge and it then becomes a resource for others to use. – Dan Temple Dec 13 '13 at 10:23

1 Answers1

30

JButton accepts HTML, so for the line break to work use:

 JButton.setText("<html>fnord<br />foo</html>");
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • 2
    Just don't use too much HTML in Swing labels. – Jakub Zaverka Nov 21 '12 at 23:07
  • See also [Word-wrap in JButtons](http://stackoverflow.com/questions/5766175/word-wrap-in-jbuttons/5767825#5767825) & [Append text in JLabel](http://stackoverflow.com/questions/9717121/append-text-in-jlabel/9717360#9717360). – Andrew Thompson Nov 21 '12 at 23:35