I'm trying to design a part of a bigger GUI that will format some text to look good. It should be able to play with text in a lot of funny ways. Adding borders, underlining, i.e anything I could want to do with text for decorative purposes. Is JTextPane the way to go for this purpose?
In my example below I want decorateTextPane()
to display two lines of text with different font. But whenever I call textPane.setFont()
I change the font of all existing text in the pane.
public class OuterClass {
InnerClass inner = new InnerClass();
private class InnerClass {
private JTextPane textPane = new JTextPane()
public InnerClass() {
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
}
public void decorateTextPane() {
inner.textPane.setFont(New Font("Times New Roman", Font.PLAIN, 15));
inner.textPane.setText("First string");
inner.textPane.setFont(New Font("Calibri", Font.PLAIN, 15));
inner.textPane.append("\nSecond string"); //my textPane class defines an append method.
}
}