Is there a word wrap property for JLabel?
Should work if you wrap the text in <html>...</html>
UPDATE:
You should probably set maximum size, too, then.
How to word wrap text in JLabel?
A common approach is to not use a JLabel
and instead use a JTextArea
with word-wrap and line-wrap turned on. You could then decorate the JTextArea to make it look like a JLabel (border, background color, etc.). [Edited to include line-wrap for completeness per DSquare's comment]
Another approach is to use HTML in your label, as seen here. The caveats there are
You may have to take care of certain characters that HTML may interpret/convert from plain text
Calling
myLabel.getText()
will now contain HTML (with possibly
escaped and/or converted characters due to #1
EDIT: Here's an example for the JTextArea approach:
import javax.swing.*;
public class JLabelLongTextDemo implements Runnable
{
public static void main(String args[])
{
SwingUtilities.invokeLater(new JLabelLongTextDemo());
}
public void run()
{
JLabel label = new JLabel("Hello");
String text = "Is There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for Jlabelaaaa";
// String text = "Is There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for JlabelIs There a "Word Wrap" Property for Jlabelaaaaaa " +
// "quick brown fox jumped over the lazy dog.";
JTextArea textArea = new JTextArea(2, 20);
textArea.setText(text);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(textArea, BorderLayout.CENTER);
frame.setSize(100,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
make a JLabel wrap it's text by setting a max width
No.
You can use HTML in the label, but then you must hard code the break tag.
A better approach is to use a JTextArea and turn wrapping on. You can change the background,foreground, font etc. of the text are to make it look like a label.
Note, this answer is outdated as of at least Java 7.
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. The text wraps as the component resizes.
Related Topics
Java: Define Terms Initialization, Declaration and Assignment
How Will Java Lambda Functions Be Compiled
Java and Gui - Where Do Actionlisteners Belong According to MVC Pattern
Java Comparison with == of Two Strings Is False
When and How to Use a Threadlocal Variable
Java 8 Iterable.Foreach() VS Foreach Loop
Should I Initialize Variable Within Constructor or Outside Constructor
Hibernate Annotations - Which Is Better, Field or Property Access
Java Code to Convert Byte to Hexadecimal
Find Java Classes Implementing an Interface
Executorservice, How to Wait for All Tasks to Finish
Jpanel in Puzzle Game Not Updating
What Is Variable Shadowing Used for in a Java Class
Reading a List from Properties File and Load with Spring Annotation @Value