Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JLabel - Show longer text as multiple lines?

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

Currently, longer lines come up as this:

enter image description here

I have to resize the window to see the complete text.

How can I make it so that there's linebreaks when the text almost reaches the width of my JFrame?

I'm not sure if any code is required here for you to answer this, 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());

The label I want to modify:

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

EDIT: More details:

I am reading lines from a file and then displaying them. The size of lines is not fixed, and so I do not know where to put <br> at.

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;
}
like image 808
user2027425 Avatar asked Feb 06 '13 20:02

user2027425


People also ask

How do I make a long text in a Jpanel?

All you need to do is surround the text with <html> and </html>, set the font size with a <font> element, and replace every \n with <br>.

How do you wrap text in JLabel?

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.

How do you fix a JLabel size?

You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height); As the Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.


2 Answers

Use HTML to display the text within the Label.

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

(Taskmaster-suggested example added in)

like image 116
arcy Avatar answered Sep 16 '22 16:09

arcy


Just another example, showing that, with the right layout manager, text wrapped in HTML tags will automatically wrap 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'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);
            }    
        });
    }        
}
like image 29
MadProgrammer Avatar answered Sep 19 '22 16:09

MadProgrammer