JLabel - Show long text as multiple lines?

So, say that I have a really long line that I want to display in JLabel . How can i do this?

Currently, longer lines are appearing:

enter image description here

I need to resize the window to see the full text.

How can I make it so that where the text almost reaches the width of my JFrame ?

I'm not sure any code is needed here so you can answer it, but still:

my frame properties:

 frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(450, 400)); frame.setLocation(new Point(400, 300)); frame.setLayout(new BorderLayout()); 

Label I want to change:

 question = new JLabel("Question:"); question.setFont(new Font("Serif", Font.BOLD, 15)); question.setHorizontalAlignment(JLabel.CENTER); 

EDIT: More Details:

I read the lines from the file and then show them. The line size is not fixed, so I don’t know where to put <br> in.

EDIT 2:

I ended up using JTextArea .

 private JTextArea textAreaProperties(JTextArea textArea) { textArea.setEditable(false); textArea.setCursor(null); textArea.setOpaque(false); textArea.setFocusable(false); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); return textArea; } 
+8
java swing jpanel jlabel
source share
4 answers

Another example showing that with the correct layout manager, the text enclosed in HTML tags is automatically transferred to the available space ...

enter image description here

 public class TestHTMLLabel { public static void main(String[] args) { new TestHTMLLabel(); } public TestHTMLLabel() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } StringBuilder sb = new StringBuilder(64); sb.append("<html>I have something to say, it beter to burn out then to fade away."). append(" This is a very long String to see if you can wrap with in"). append("the available space</html>"); JLabel label = new JLabel(sb.toString()); JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(label); frame.setSize(100, 100); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } 
+23
source share

Use HTML to display text inside the label.

 JLabel fancyLabel = new JLabel("<html>Punch Taskmaster</html>"); 

(added example suggested by Taskmaster)

+10
source share

Format with HTML. It works great.

 import javax.swing.*; import java.awt.*; public class Test { public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(450, 400)); frame.setLocation(new Point(400, 300)); frame.setLayout(new BorderLayout()); final JLabel question = new JLabel("<html>Question:<br>What is love?<br>Baby don't hurt me<br>Don't hurt me<br>No more</html>"); question.setFont(new Font("Serif", Font.BOLD, 15)); question.setHorizontalAlignment(JLabel.CENTER); frame.add(question); frame.setVisible(true); } } 
+5
source share

Something like that. The answer received by rcook is very correct. Its just an example to show how this can be done.

  b1 = new JLabel("<html>Default Lable I have to resize the <br/> window to see the complete text.</html>"); 
+4
source share

All Articles