Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 639
k0pernikus Avatar asked Nov 21 '12 22:11

k0pernikus


People also ask

How do I add a border to a JButton?

We can set different borders like LineBorder, BevelBorder, EtchcedBorder, EmptyBorder, TitledBorder, etc to JButton using the setBorder() method of JComponent class.

How do I change the text of a JButton?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.

What does JButton do in Java?

The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.

What is difference between JButton button?

JButton class provided by Swing is totally different from Button which is provided by AWT. It is used to add platform independent button in swing application. Swing buttons are extended from the AbstractButton class that is derived from JComponent.


1 Answers

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

 JButton.setText("<html>fnord<br />foo</html>");
like image 199
k0pernikus Avatar answered Nov 15 '22 13:11

k0pernikus