Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a multilined JLabel (or a JTextArea looking totally the same) without HTML

I cant believe fastest solution for a multilined JLabel is the following one (text comes from a var, and so I dont want to put HTML code manually every x chars, its so ugly):

public class JMultilineLabel extends JTextArea{
    private static final long serialVersionUID = 1L;
    public JMultilineLabel(String text){
        super(text);
        setEditable(false);  
        setCursor(null);  
        setOpaque(false);  
        setFocusable(false);  
        setFont(UIManager.getFont("Label.font"));      
        setWrapStyleWord(true);  
        setLineWrap(true);
    }
} 

... sure it isnt a better way to handle this????

like image 859
Whimusical Avatar asked Jun 14 '12 13:06

Whimusical


People also ask

Can a JLabel display an image?

A JLabel object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area.

Is JTextArea editable?

The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.

How do you create a JLabel image?

You have to supply to the JLabel an Icon implementation (i.e ImageIcon ). You can do it trough the setIcon method, as in your question, or through the JLabel constructor: Image image=GenerateImage. toImage(true); //this generates an image file ImageIcon icon = new ImageIcon(image); JLabel thumb = new JLabel(); thumb.

What is the difference between a JTextField and a JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.


1 Answers

If you want a multilne label then you simply use HTML in its text, as they support its use. Therefore, use line brake tag </br> to break lines or put separate lines in <p></p> paragraph tags.

Do not forget to mark that you want to use HTML for a JLabel by starting its text with the <html> tag.

More available here.

BTW I forgot to check if there were other related questions about JLabel use and there were at least a few, check this or this. :)


EDIT:

For a working sample, showing a different approach, without setting a style and with use of paragraph and label taking available space, please see below:

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;

public class LabelHTMLAutoResize {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel p = new JPanel(new BorderLayout());
                JLabel l = new JLabel("<html><p> Some verrrry long text  Some verrrry long  Some verrrry long text dsa ads oiosi o</p>");
                l.setVerticalAlignment(SwingConstants.TOP);
                l.setOpaque(true);
                l.setBackground(Color.green);
                p.add(l);
                f.setContentPane(p);
                /* good practice is to use f.pack(); and let the size be automatically calculated but we want to show line wrapping thus frame size is set */
                f.setSize(200, 200);
                f.setVisible(true);
            }
        });
    }
}
like image 127
Boro Avatar answered Nov 09 '22 09:11

Boro