JLabel - Show longer text as multiple lines?
Just another example, showing that, with the right layout manager, text wrapped in HTML
tags will automatically wrap to the available space...
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's 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);
}
});
}
}
JLabel with multiple lines and alignment to the right
Slightly simpler HTML than seen in @MadProgrammer's answer:
new JLabel("<html><body style='text-align: right'>Search<br>By:");
Java Label with multiple lines
Wrap it in HTML Tags and use <br>
instead of \n
:
jLabel1.setText("<html>Gib die Zeit,<br> in der der Computer heruntergefahren werden soll in Sekunden, Minuten und Stunden an und drücke dann auf Herunterfahren</html>");
Creating a JLabel with multiple lines from other text
Simplest solution: don't use a JLabel. Place your text into a JTextArea that looks like a JLabel -- that is not opaque, focusable nor editable.
e.g.,
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class AreaAsLabel extends JPanel {
private JTextArea textEntry = new JTextArea(5, 20);
private JTextArea labelLikeDisplay = new JTextArea(5, 20);
public AreaAsLabel() {
textEntry.setLineWrap(true);
textEntry.setWrapStyleWord(true);
labelLikeDisplay.setLineWrap(true);
labelLikeDisplay.setWrapStyleWord(true);
labelLikeDisplay.setEditable(false);
labelLikeDisplay.setFocusable(false);
labelLikeDisplay.setOpaque(false);
add(new JScrollPane(textEntry));
add(new JButton(new TransferTextAction("Transfer Text")));
add(labelLikeDisplay);
}
class TransferTextAction extends AbstractAction {
public TransferTextAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = textEntry.getText();
labelLikeDisplay.setText(text);
textEntry.selectAll();
textEntry.requestFocusInWindow();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("AreaAsLabel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new AreaAsLabel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
How to print setText() on multiple lines of a JLabel?
Try using html tags:
String txt = "<html>";
for (Element tag : tags) {
txt += tag.text() + "<br/>";
}
txt += "</html>";
label.setText(txt);
Java swing: Multiline labels?
You can use HTML
in JLabels
. To use it, your text has to start with <html>
.
Set your text to "<html>This is<br>a multi-line string"
and it should work.
See Swing Tutorial: JLabel and Multiline label (HTML) for more information.
Related Topics
How to Create and Handle Composite Primary Key in JPA
Why Does the Jvm Still Not Support Tail-Call Optimization
How to Generate Jaxb Classes from Xsd
Allowing Java to Use an Untrusted Certificate for Ssl/Https Connection
How to Make Threadpoolexecutor's Submit() Method Block If It Is Saturated
Comparing Two Java.Util.Dates to See If They Are in the Same Day
Warning - Build Path Specifies Execution Environment J2Se-1.4
Is an Array a Primitive Type or an Object (Or Something Else Entirely)
Collection to Stream to a New Collection
How Can Non-Ascii Characters Be Removed from a String
Java Gui Frameworks. What to Choose? Swing, Swt, Awt, Swingx, Jgoodies, Javafx, Apache Pivot
Maven - Always Download Sources and Javadocs
Difference Between Spring @Controller and @Restcontroller Annotation
How to Catch Out of Memory Exception in Java
How to Sort a List by Different Parameters at Different Timed
How to Handle Very Large Numbers in Java Without Using Java.Math.Biginteger