How can I make the text of a JLabel extend onto another line?
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.
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.
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);
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