Forum Stats

  • 3,757,263 Users
  • 2,251,216 Discussions
  • 7,869,781 Comments

Discussions

Disabling word wrap for JTextPane

2

Comments

  • camickr
    camickr Member Posts: 24,931
    I tried adding the getMinimumSpan code to the class I posted above. It still doesn't work 100 percent.

    Type in the following data

    a) two tab characters
    b) "one two three four..." until the scroll bar appears
    c) then use the enter key to add a new line (the scrollbar disappears)
    d) start typing on the new line and the scrollbar will reappear

    The view has problems when tab characters are found in a preceeding line.

    This is the same basic problem with the suggestion I provided 3 years ago.
  • 843804
    843804 Member Posts: 45,751
    After a grueling 3 days of looking at swing code, I've finally found the solution!!!
    check the following code out. No need to set the bounds to any specific size, will work for any line length, well, reasonablly long. No problems with tabs or newlines either. The only thing I can't think of solving is the caret not being visible when editing the right most part of the text area. Any suggestions?



    //NoWrapTextPane.java

    import java.awt.Dimension;

    import javax.swing.JTextPane;
    import javax.swing.text.EditorKit;

    public class NoWrapTextPane extends JTextPane {
    public boolean getScrollableTracksViewportWidth() {
    //should not allow text to be wrapped
    return false;
    }

    public void setSize(Dimension d) {
    //dont let the Textpane get sized smaller than its parent
    Dimension pSize = getParent().getSize();
    if (d.width < pSize.width) {
    super.setSize(pSize.width, d.height);
    } else {
    super.setSize(d);
    }
    }

    protected EditorKit createDefaultEditorKit() {
    return new MyEditorKit();
    }

    }


    //MyEditorKit.java

    import javax.swing.text.*;

    public class MyEditorKit extends StyledEditorKit {
    private static final ViewFactory defaultFactory = new MyViewFactory();
    public ViewFactory getViewFactory() {
    return defaultFactory;
    }


    public static class MyViewFactory implements ViewFactory {
    public View create(Element elem) {
    String kind = elem.getName();
    if (kind != null) {
    if (kind.equals(AbstractDocument.ContentElementName)) {
    return new MyView(elem);
    } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
    return new ParagraphView(elem);
    } else if (kind.equals(AbstractDocument.SectionElementName)) {
    return new BoxView(elem, View.Y_AXIS);
    } else if (kind.equals(StyleConstants.ComponentElementName)) {
    return new ComponentView(elem);
    } else if (kind.equals(StyleConstants.IconElementName)) {
    return new IconView(elem);
    }
    }

    // default to text display
    return new MyView(elem);
    }
    }


    public static class MyView extends LabelView {
    public MyView(Element e) {
    super(e);
    }

    public float getTabbedSpan(float x, TabExpander e) {
    float result = super.getTabbedSpan(x, e);
    this.preferenceChanged(this, true, false);
    return result;
    }

    }
    }
  • 843804
    843804 Member Posts: 45,751
    To camickr.

    I investigated this a little more. Somehow the TabExpander is null but in all editor kits ParagraphView implements TabExpander. So I added a check if the expander==null use parent ParagraphView as expander.

    That's a simple implementation of LabelView. I used 0 instead of real X of label (last parameter). We can also override gettabExpander() of LabelView to return parent paragraph if the expander isn't set.

    regards,
    Stas
        static class MyLabelView extends LabelView {
            public MyLabelView(Element elem) {
                super(elem);
            }
            public float getPreferredSpan(int axis) {
                float span=0;
                if (axis==View.X_AXIS) {
                    int p0 = getStartOffset();
                    int p1 = getEndOffset();
                    checkPainter();
                    TabExpander ex=getTabExpander();
                    if (ex==null) {
                        //paragraph implements TabExpander
                        ex=(TabExpander)this.getParent().getParent();
                    }
                    span = getGlyphPainter().getSpan(this, p0, p1, ex, 0);
                    return Math.max(span, 1);
                }
                else {
                    span=super.getPreferredSpan(axis);
                }
                return span;
            }
        }
  • 843804
    843804 Member Posts: 45,751
    To battbot.
    That's really bad to call preferenceChanged in the getting tabbed span.
    That means every time when you measure your label you relayout all content. I think the component will be really slow.

    regards,
    Stas
  • 843804
    843804 Member Posts: 45,751
    I think your solution is better Stas. Thanks alot!! Now if you can also help me figure out how to make the cursor show up when it's all the way to the right, then we will have a workable JTextPane on our hands.

    Frank
  • 843804
    843804 Member Posts: 45,751
    Oh btw Stas, in which cases are the tab expansion based on the x position? Will setting the x position to 0 rather than it's true position cause any unwanted side-effects?

    Frank
  • camickr
    camickr Member Posts: 24,931
    Now if you can also help me figure out how to make the cursor show up when it's all the way to the right,
    This is also the default behaviour when you use a JTextArea that does not wrap, so it may not be as easy to fix.

    But I'm thinking you would play with the span of every line and add a few pixels to the calculated amount to allow for the caret to be painted.
  • 843804
    843804 Member Posts: 45,751
    It's better init tab expander and use super method.

    Like this.

    As for cursor If you scroll slightly to the right the caret becomes visible. At first glance we can just scroll one-two pixels to the right...

    Need more investigations... ot you can try to add a few pixels to right inset of SectionView.

    regards,
    Stas
            public float getPreferredSpan(int axis) {
                if (axis==View.X_AXIS) {
                    TabExpander ex=getTabExpander();
                    if (ex==null) {
                        //paragraph implements TabExpander
                        ex=(TabExpander)this.getParent().getParent();
                        getTabbedSpan(0, ex);
                    }
                }
                return super.getPreferredSpan(axis);
            }
  • 843804
    843804 Member Posts: 45,751
    Thanks for all the responses Stas and Camickr. You get the duke dollars Stas, thnx for the help.

    Frank
  • 843804
    843804 Member Posts: 45,751
    Hi.

    Is there a way to overwrite the layout method in BlockView class without overwriting the create method in ViewFactory class?

    Thnx.
This discussion has been closed.