0

I have some problems adding a text at the bottom of a JTextPane. At all the research i made the only solution i found was to add the JTextPane into a JPanel. My problem is that i have also a code to wrap the text lines. The combine of text wrapping and adding the JTextPane into a JPanel isnt working. Currently i have the text wrap thing on my code, and here it is:

package com.models;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

import com.Config;

public class TextPane extends JTextPane
{
    private static final long serialVersionUID = -8251523472372069488L;

    private StyledDocument doc;
    private Style style;

    public TextPane()
    {
        DefaultCaret caret = (DefaultCaret) getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        setEditable(false);
        setFont(new Font("Lucida Console", Font.PLAIN, 12));
        setBackground(Color.LIGHT_GRAY);
        setCaret(caret);
        setEditorKit(new WrapEditorKit());

        doc = getStyledDocument();
        style = addStyle("Style", null);
    }

    public void append(Color color, String text)
    {
        StyleConstants.setForeground(style, color);

        try
        {
            doc.insertString(doc.getLength(), Config.LINE_SEPARATOR + text, style);
        }
        catch (BadLocationException e)
        {}
    }

    private class WrapEditorKit extends StyledEditorKit
    {
        private static final long serialVersionUID = -8173946569665645524L;

        final ViewFactory defaultFactory = new WrapColumnFactory();

        public ViewFactory getViewFactory()
        {
            return defaultFactory;
        }
    }

    private class WrapColumnFactory implements ViewFactory
    {
        public View create(Element elem)
        {
            final String kind = elem.getName();
            if (kind != null)
            {
                switch (kind)
                {
                case AbstractDocument.ContentElementName:
                    return new WrapLabelView(elem);
                case AbstractDocument.ParagraphElementName:
                    return new ParagraphView(elem);
                case AbstractDocument.SectionElementName:
                    return new BoxView(elem, View.Y_AXIS);
                case StyleConstants.ComponentElementName:
                    return new ComponentView(elem);
                case StyleConstants.IconElementName:
                    return new IconView(elem);
                default:
                    return new LabelView(elem);
                }
            }

            return new LabelView(elem);
        }
    }

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

        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);
            }
        }
    }
}

My question is: How i can have text wrapping and also insert a text at the bottom of the JTextPane

2
  • >text wrapping and adding the JTextPane into a JPanel isnt working. How to expect it to be working?
    – StanislavL
    Feb 6, 2015 at 5:38
  • Creating a JPanel with BorderLayout and adding the JTextPane at the south of the JPanel will do the trick. Text will insert at the bottom. The problem is that this trick will not work with the text wrapping code that i already have.
    – Charus
    Feb 6, 2015 at 11:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.