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.