Constantin Müller
Constantin Müller

Reputation: 1290

JTextPane won't display text added by DefaultStyledDocument

Okay I've the following problem, I write a simple graphical chat client with the Java Swing Framework. For displaying the received messages I use a JTextPane. I had the problem that when one user sends a message out of single character without any blanks, the JTextPane component doesn't wrap the line. I solved this issue with the following code, now the JTextPane component don't wrap only by word boundaries, also at any character if the length don't fit the width of the component.

public class WrapEditorKit extends StyledEditorKit
{
    ViewFactory defaultFactory;

    public WrapEditorKit()
    {
        this.defaultFactory = new WrapColumnFactory();
    }

    public ViewFactory getViewFactory()
    {
        return this.defaultFactory;
    }

}

class WrapLabelView extends LabelView
{
    public WrapLabelView(Element element)
    {
        super(element);
    }

    public float getMinimumSpan(int axis)
    {
        switch(axis)
        {
            case View.X_AXIS:
            {
                return 0;
            }
            case View.Y_AXIS:
            {
                return super.getMinimumSpan(axis);
            }
            default:
            {
                throw new IllegalArgumentException("Invalid axis: " + axis);
            }
        }
    }
}

class WrapColumnFactory implements ViewFactory
{
    public View create(Element element)
    {
        String kind = element.getName();

        if(null != kind)
        {
            if(kind.equals(AbstractDocument.ContentElementName))
            {
                return new WrapLabelView(element);
            }
            else if(kind.equals(AbstractDocument.ParagraphElementName))
            {
                return new ParagraphView(element);
            }
            else if(kind.equals(AbstractDocument.SectionElementName))
            {
                return new BoxView(element, View.Y_AXIS);
            }
            else if(kind.equals(StyleConstants.ComponentElementName))
            {
                return new ComponentView(element);
            }
            else if(kind.equals(StyleConstants.IconElementName))
            {
                return new IconView(element);
            }
        }

        return new LabelView(element);
    }
}

I got this code for a long time from a web page, I don't have any URL, I it works great for a small text edit panel. But when I add text over a Document like above it displays nothing, it works only if I type the words directly in the pane, but not when I add text over a method of the Document class...

StyleContext msgPaneStyle = new StyleContext();
final DefaultStyledDocument msgPaneDocument = new DefaultStyledDocument(this.msgPaneStyle);
JTextPane msgPane = new JTextPane(msgPaneDocument);
msgPane.setEditorKit(new WrapEditorKit());

If I add text now by typing..

msgPaneDocument.insertString(msgPaneDocument.getLength(), text, null);

...it doesn't work (Doesn't display the text), without the editor kit it works. Any ideas or hints?

EDIT

I think the problem is, that my custom EditorKit and the StyledDocument don't work at same time... If I insert text by typing msgPane.setText(text) it works!

Upvotes: 1

Views: 225

Answers (1)

Constantin Müller
Constantin Müller

Reputation: 1290

I solved/prevented this issue myself. When I'm using...

JTextPane msgPane = new JTextPane();
msgPane.setEditorKit(new WrapEditorKit());

msgPane.getDocument().insertString(msgPane.getDocument().getLength(), text, null);

...it works, also with highlighting of single words! I don't add a custom DefaultStyledDocument this time, instead I used the Document returned by msgPane.getDocument() and now it works.

If there are any other solutions, specially with using of a custom DefaultStyledDocument or any explanations for this issue I would be glad...

Upvotes: 2

Related Questions