JEditorPane linewrap in Java7

前端 未结 2 1357
醉话见心
醉话见心 2021-01-01 04:01

First of all I hope it\'s not a problem I started a new topic. Tbh I don\'t have a clue how to ask a question based on an already answered one, so I made this.

I\'m

2条回答
  •  天涯浪人
    2021-01-01 04:18

    A deadly better solution I found : The
    is correctly handled by the HTMLEditorKit, but the Patrick Sebastien's post mentionned that it won't. It's because its ViewFactory threat all InlineView object as wrappable, but the BRView is also an InlineView. See my solution below:

    class WrapColumnFactory extends HTMLEditorKit.HTMLFactory {
    
            @Override
            public View create(Element elem) {
                View v = super.create(elem);
    
                if (v instanceof LabelView) {
    
                    // the javax.swing.text.html.BRView (representing 
    tag) is a LabelView but must not be handled // by a WrapLabelView. As BRView is private, check the html tag from elem attribute Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute); if ((o instanceof HTML.Tag) && o == HTML.Tag.BR) { return v; } return new WrapLabelView(elem); } return v; } } class WrapLabelView extends LabelView { public WrapLabelView(Element elem) { super(elem); } @Override 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); } } }

提交回复
热议问题