I’ve been trying to create a class which can do the following:
• Set: Font, Alignment (left, center, right, justified)
• An efficient way to append text to the document.
The text does not need to be selectable or editable.
I have to be able to override the painting / rendering of the text.
I find that the JDK JTextComponent classes are difficult to use efficiently, as this is what I have so far but it is far from what I’m trying to achieve:
public class Paragraph extends JTextPane{
public Paragraph(){
this.setFont(Fonts.PARAGRAPH);
this.setOpaque(false);
}
// ridiculously slow
public void append(String s) {
SimpleAttributeSet def = new SimpleAttributeSet();
StyleConstants.setForeground(def, Colors.PARAGRAPH);
Document d = getDocument();
try {
d.insertString(d.getLength(), s, def);
} catch (BadLocationException ble) {
}
}
}
Question: Are there any libraries which could save me time re-inventing the wheel?
If not, how can I go about extending the JDK implementations? Thanks
Your Document should be StyledDocument instance. Then just use setParagraphAttributes() method of the Document instance.
For multiple appends use a separate document (not set to the JTextPane instance.
Use the kit ot create a new empty Document instance. Call all your appends and then setDocument(theDocInstance) to the JTextPane.