Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a newline to JLabel without using HTML

How can I add a new line to a JLabel? I know if I use simple HTML, it will work. But if I use HTML, JLabel is not showing the font which embedded with the application. I am embedding the font using the method - createFont() and using JLabel.setFont() for applying the font.

like image 832
Nauphal Avatar asked Sep 07 '11 10:09

Nauphal


2 Answers

SwingX supports multiline labels:

   JXLabel label = new JXLabel();
   label.setLineWrap(true);
like image 61
kleopatra Avatar answered Nov 02 '22 04:11

kleopatra



I don't think there is direct(and easy) way of doing JLabel with multiple lines without recurring to HTML. You can use JTextArea instead.

JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setBorder(BorderFactory.createEmptyBorder());
add(textArea, BorderLayout.CENTER);

It should look almost the same. If you have different fonts for different components, you can add the following line to ensure that the font of JTextArea is the same with JLabel

textArea.setFont(UIManager.getFont("Label.font"));

Hope this helps.

like image 33
Serhiy Avatar answered Nov 02 '22 03:11

Serhiy