开发者

Java: Linebreaks in JLabels?

I'm trying to make a Swing JLabel with multiple lines of text. It's added just fine, but the line breaks don't come through. How do I do this? Alternatively, can I just specify a maximum width for a JLabel and know th开发者_StackOverflowat the text would wrap, like in a div?

    private void addLegend() {
        JPanel comparisonPanel = getComparisonPanel();

        //this all displays on one line
        JLabel legend = new JLabel("MMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\n"); 

        comparisonPanel.add(legend);        
    }


Use HTML in setText, e.g.

myLabel.setText("<html><body>with<br>linebreak</body></html>");


You can get automatic line break if you set the paragraph width in html.

  label.setText("<html><p style=\"width:100px\">"+paragraph+"</p></html>");


By default, Swing does not wrap text. If you specify a size on the JLabel it will only paint the part of the text that fits and then add "..." to the end.

As suggested you can use HTML to enable line wrapping. However, I've actually created a custom Swing UI delegate not long ago to achieve this and even more: MultiLineLabelUI.

It will wrap your text to fit the available space and also respect hard line breaks. If you choose to try it out, it is as simple as:

JLabel label = new JLabel("Text that'll wrap if necessary");
label.setUI(MultiLineLabelUI.labelUI);

Or alternatively use the custom MultiLineLabel class that in addition to wrapping text supports vertical and horizontal text alignment.

UPDATE

I lost the domain with the original code samples. It can now be viewed on github instead: https://github.com/sasjo/multiline


You can put HTML inside of a JLabel and use the linebreak tag to achieve this.


What about using the wrapping feature in a JTextArea?

    String text = "some really long string that might need to"+
                  "be wrapped if the window is not wide enough";

    JTextArea multi = new JTextArea(text);
    multi.setWrapStyleWord(true);
    multi.setLineWrap(true);
    multi.setEditable(false);

    JLabel single = new JLabel(text);

    JPanel textpanel = new JPanel(new GridLayout(2,1));
    textpanel.add(multi);
    textpanel.add(single);

    JFrame frame = new JFrame();
    frame.add(textpanel);
    frame.pack();
    frame.setVisible(true);


Simple,use HTML. Java Swing components though does not provide a 'fantastic' support for the HTML, you can use it for such simple purposes.

label.setText("<html>This is first line.<br/>This is second line.</html>");


I did not manage to specify a maximum width for a label but you can specify a concrete width. By measuring the current width of a JLabel we can only apply the new fixed width if the JLabels's width is higher that our maxWidth:

JLabel label = new JLabel("<html>" + myVeryLongMessage + "<html>");
int maxWidth = 400;
Dimension size = label.getPreferredSize();
if (size.width > maxWidth) {
    // Estimate the number of lines
    int lineCount = (int) Math.ceil(((double) size.width) / maxWidth);
    lineCount += 1; // Add one extra line as reserve
    size.width = maxWidth; // Apply the maximum width
    // Increase the height so that all lines will be visible
    size.height *= lineCount;
    label.setPreferredSize(size);
}


You can use a JTextArea and disable the TextArea, this way, you will only display what you want, and the user won't be able to type in

JTextArea area = new JTextArea("Here \n\n you \n\n put \n\n your \n\n text");
area.setBounds(10, 11, 500, 143);
area.setEditable(false);
yourPannel.add(area);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜