I'm using a JTextPane as an alternate to JTextArea because I want the inner text to be centered. Unfortunately, I'm having trouble because, when I place the JTextPane inside a panel instead of inside the frame directly, the text is not wrapping like it does when I place it directly inside the frame. What can I do to make the JTextPane to wrap like normal?
package sscce;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.View;
/*
* A collection of static methods that provide added functionality for
* text components (most notably, JTextArea and JTextPane)
*
* See also: javax.swing.text.Utilities
*/
public class SSCCE
{
public static JTextArea createTextArea(final int cols, final String text)
{
final JTextArea t = new JTextArea(1, cols);
t.setLineWrap(true);
t.setWrapStyleWord(true);
t.setSize(0, Short.MAX_VALUE);
t.setFont(new Font("Courier New", Font.BOLD, 12));
t.setText(text);
t.setEditable(false);
return t;
}
public static JTextPane createTextPane(final String string)
{
final StyleContext context = new StyleContext();
final StyledDocument document = new DefaultStyledDocument(context);
final Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
final JTextPane textPane = new JTextPane(document);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
textPane.setFont(new Font("Courier New", Font.BOLD, 12));
textPane.setText(string);
textPane.setEditable(false);
return textPane;
}
public static int getWrappedLines(final JTextArea component)
{
final View view = component.getUI().getRootView(component).getView(0);
final int preferredHeight = (int) view.getPreferredSpan(View.Y_AXIS);
final int lineHeight =
component.getFontMetrics(component.getFont()).getHeight();
return preferredHeight / lineHeight;
}
public static void main(final String args[])
{
final JTextArea ta =
createTextArea(15,
"This is a JTextArea that should wrap automatically.");
final JTextPane tp =
createTextPane("This is a JTextPane text that should wrap " +
"automatically.");
final JTextPane tpFrame =
createTextPane("This is a JTextPane text that should wrap " +
"automatically.");
final JFrame f = new JFrame("SSCCE");
final JPanel panel = new JPanel(new GridLayout(2, 1));
final JPanel p[] = {new JPanel(), new JPanel()};
p[0].add(ta);
p[1].add(tp);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(p[0]);
panel.add(p[1]);
f.add(panel, BorderLayout.CENTER);
f.add(tpFrame, BorderLayout.SOUTH);
f.setSize(100, 300);
f.setVisible(true);
try
{
Thread.sleep(1000);
}
catch (final InterruptedException e)
{
e.printStackTrace();
}
System.out.println(getWrappedLines(ta));
}
}