Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline text in JLabel

Tags:

java

jlabel

How can I make the text of a JLabel extend onto another line?

like image 929
Jessy Avatar asked Mar 26 '09 12:03

Jessy


People also ask

How do you wrap text in JLabel?

As per @darren's answer, you simply need to wrap the string with <html> and </html> tags: myLabel. setText("<html>"+ myString +"</html>"); You do not need to hard-code any break tags.

Is JLabel editable?

It is impossible to make an editable label as it exists. You can use a jTextField with no border and same background as the JFrame. Having said that, you can add a KeyListener to your JLabel and update your label's text depending on the key that was pressed.


1 Answers

You can do it by putting HTML in the code, so:

JFrame frame = new JFrame(); frame.setLayout(new GridLayout()); JLabel label = new JLabel("<html>First line<br>Second line</html>"); frame.add(label); frame.pack(); frame.setVisible(true); 
like image 92
tddmonkey Avatar answered Oct 09 '22 07:10

tddmonkey