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.
SwingX supports multiline labels:
JXLabel label = new JXLabel();
label.setLineWrap(true);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With