New Line \n is not working in JButton.setText("fnord\nfoo") ;

k0pernikus picture k0pernikus · Nov 21, 2012 · Viewed 19.3k times · Source

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?

Answer

k0pernikus picture k0pernikus · Nov 21, 2012

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

 JButton.setText("<html>fnord<br />foo</html>");