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

20,896

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

 JButton.setText("<html>fnord<br />foo</html>");
Share:
20,896
k0pernikus
Author by

k0pernikus

I am Philipp Kretzschmar, a backend developer from Hamburg working at Demvsystem. You can find me on github. Or twitter. My main weapons of choice are: php TypeScript (I don't want to write plain JavaScript anymore) and nodejs I play around with: Java python rust I used to write some scala, but for now I don't want to go there anymore. I feel most comfortable on a unix-like system featuring a powerful bash. (This excludes MacOS.) I love to code within JetBrains's flavored IDEs, e.g. IntelliJ, PhpStorm, WebStorm and using the IdeaVim plugin and having a docker-compose stack to develop on.

Updated on November 22, 2020

Comments

  • k0pernikus
    k0pernikus over 3 years

    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?

  • Jakub Zaverka
    Jakub Zaverka over 11 years
    Just don't use too much HTML in Swing labels.
  • Andrew Thompson
    Andrew Thompson over 11 years