1

I have a problem with JLabel and plain text wrapping in it. For plain text wrapping i am using LabelView and i am not very happy about the result it gives to me.

My goal is to get plain text wrapping as in for example JTextArea. I tried WrappedPlainView but it works only with JTextComponent

So my question would be maybe you guys know/or have some advance JLabel views?

note: i do not want to add html to my plain text.

LabelView: http://docs.oracle.com/javase/6/docs/api/javax/swing/text/LabelView.html

WrappedPlainView: http://docs.oracle.com/javase/6/docs/api/javax/swing/text/WrappedPlainView.html

Try this example and look how words are wrapped when frame is resized:

import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
private static final String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                                        "Ut in enim velit. Nunc posuere purus ac odio dictum auctor. " +
                                        "Vivamus nec sem mi. Curabitur sed iaculis nibh. Proin vel massa augue. " +
                                        "Aenean laoreet, tellus ut vulputate mollis, justo eros ornare tortor, " +
                                        "vitae venenatis turpis augue id purus. In quis pretium justo. " +
                                        "Quisque interdum sem at augue ultrices molestie. " +
                                        "Nulla consectetur magna nec est malesuada sed ultricies diam gravida. " +
                                        "Curabitur luctus, nulla nec pulvinar fringilla, enim turpis luctus tellus, " +
                                        "non auctor diam ligula quis lectus.";

public Example()
{
    JTextArea textArea = new JTextArea(LONG_TEXT);
    textArea.setWrapStyleWord(true);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setOpaque(false);
    textArea.setEditable(false);

    add(new JScrollPane(textArea), BorderLayout.CENTER);
    setMinimumSize(new Dimension(100, 100));
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
}

public static void main(String []args)
{
    new Example();
}
}

Take a look to this example and you will see the problem with JLabel wrapping words:

import javax.swing.*;
import java.awt.*;
public class Example extends JFrame {
private static final String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                                        "Ut in enim velit. Nunc posuere purus ac odio dictum auctor. " +
                                        "Vivamus nec sem mi. Curabitur sed iaculis nibh. Proin vel massa augue. " +
                                        "Aenean laoreet, tellus ut vulputate mollis, justo eros ornare tortor, " +
                                        "vitae venenatis turpis augue id purus. In quis pretium justo. " +
                                        "Quisque interdum sem at augue ultrices molestie. " +
                                        "Nulla consectetur magna nec est malesuada sed ultricies diam gravida. " +
                                        "Curabitur luctus, nulla nec pulvinar fringilla, enim turpis luctus tellus, " +
                                        "non auctor diam ligula quis lectus.";

public Example()
{
    JLabel label = new JLabel(LONG_TEXT);
    add(label, BorderLayout.CENTER);
    setMinimumSize(new Dimension(100, 100));
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
}

public static void main(String []args)
{
    new Example();
}
}

So the goal is to get JLabel words wrapping as in JTextArea.

NOTE: in real project i am working with javax.swing.text.View`s and i am using LabelView for plain text.

Mode
  • 61
  • 1
  • 6
  • For better help sooner, post an [SSCCE](http://sscce.org/). ;) – Andrew Thompson May 17 '13 at 09:50
  • 1
    @Mode Already after your edit to the question it is not easy to give you advice. Please consider Andrew's comment and prepare a SSCCE. Also show and describe what you do not like about `LabelView` and what instead you want to achieve. – Howard May 17 '13 at 09:52
  • @Mode see my helicopter view in my 1st reall attempt – mKorbel May 17 '13 at 10:57

4 Answers4

3

So my question would be maybe you guys know/or have some advance JLabel views,

there are two ways,

  • use JTextComponent

    1. non_editable, setEditable()
    2. change setDisabledTextColor()
    3. (only if needded) for transparency (JLabel is by default) you can to change opacity

.

  • use JLabel with Html (reduced and implemented up_to version Html3.2)

  • post and SSCCE

EDIT:

NOTE: in real project i am working with javax.swing.text.View`s and i am using LabelView for plain text.

for JTextComponents and javax.swing.text.Xxx is required to use intial PreferredSize, see public JTextArea(String text, int rows, int columns)

add 1) use JTextComponent

there must be decision about

  • if wrapped in JScrollPane without or wihtout visible JScrollBar(override MouseScroll)

  • or simple added to container, I'd suggest to use BorderLayout or GridLayout, then JTextArea is resizable (looks like as) with container too

  • scrollable JComponents will be added to JScrollPane

.

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class ExampleJTextArea extends JFrame {

    private /*static*/ final String LONG_TEXT = "Lorem ipsum dolor sit amet, "
            + "consectetur adipiscing elit. Ut in enim velit. Nunc posuere "
            + "purus ac odio dictum auctor. Vivamus nec sem mi. Curabitur sed "
            + "iaculis nibh. Proin vel massa augue. Aenean laoreet, tellus ut "
            + "vulputate mollis, justo eros ornare tortor, vitae venenatis "
            + "turpis augue id purus. In quis pretium justo. Quisque interdum "
            + "sem at augue ultrices molestie. Nulla consectetur magna nec est "
            + "malesuada sed ultricies diam gravida. Curabitur luctus, nulla "
            + "nec pulvinar fringilla, enim turpis luctus tellus, non auctor "
            + "diam ligula quis lectus.";

    public ExampleJTextArea() {
        JTextArea textArea = new JTextArea(LONG_TEXT, 10, 25);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setOpaque(false);
        textArea.setEditable(false);
        textArea.setBorder(new EmptyBorder(10,10,2,2));
        add(/*new JScrollPane(*/textArea/*), BorderLayout.CENTER*/);
        //setMinimumSize(new Dimension(100, 100));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ExampleJTextArea exampleJTextArea = new ExampleJTextArea();
            }
        });
    }
}

.

.

add 2) use JLabel with Html (reduced and implemented up_to version Html3.2), and another variations

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • :-) whats doesn't work, this was reason to called that as 1st. attempt :-) – mKorbel May 17 '13 at 11:02
  • The example only shows what i want to get. Working with Views i "do not have JComponents". I have Views for example LabelView which is added to Shape for representing text. – Mode May 17 '13 at 11:06
  • time to back from the end to the start, 1. forgot for javax.swing.text. 2. forgot for JLabel, JTextComponent, 3. now to test (a few times on this forum) methods implemented in SwingUtilities (used for internal calculations), starting with compute and layout 4. then is time to decide which of JComponent is better, – mKorbel May 17 '13 at 11:19
  • In short i need mix of WrappedPlainView + LabelView. – Mode May 17 '13 at 13:41
  • Because WrappedPlainView can work with multi line as JTextArea LabelView can work with StyledDocument.. – Mode May 17 '13 at 13:42
1

A very old thread, but in case anyone comes across this problem again...

None of these solutions seemed to work for me (perhaps it is because the JPanel that contained my JLabel was viewed by a JScrollPane?), and I couldn't get JTextAreas working because it has some sort of fiddly bug where it grows but won't shrink.

Instead, here is the easiest solution I could find for wrapping text in a JLabel, provided by Tony Fecteau on this page: Just use setPreferredSize() on a html-formatted JLabel and it'll start automatically growing and shrinking!

panel = new JPanel();
JLabel label = new JLabel("<html>" + "long text to display" + "</html>");
label.setPreferredSize(new Dimension(1, 1)); // this is the key part.
panel.add(label, "grow");
Jamie Birch
  • 5,839
  • 1
  • 46
  • 60
0

I also agree with @mKorbel that there are two ways to achieve the proper wrapping words through JLabel:

a) Create your own custom component that emulates the JLabel style while wrapping.

public class CustomWrappedLabel extends JTextArea {
    public CustomWrappedLabel(String text) {
        super(text);
        setBackground(null);
        setEditable(false);
        setBorder(null);
        setLineWrap(true);
        setWrapStyleWord(true);
        setFocusable(false);
    }
}

Usage:

new CustomWrappedLabel("Hey you've just tried multi-line text!");

Make sure that you set the amount of rows more than 2 lines to achieve the required behavior. wrappedLabelObj.setRows(2). Use pack() to calculate the height of the parent component correctly.

b) Use html which automatically take care of wrapped text inside <html> tag.

new JLabel("<html>Hey you've just tried html multi-line text version!</html>")

You can use <br /> for newline.

Hope this helps!

Deminem
  • 672
  • 8
  • 19
0

try this

jLabel.setText(String.format("<html><body style=\"text-align: justify;  text-justify: inter-word;\">%s</body></html>",LONGTEXT));