Question

My program has a problem.. It is WordWrap is abled...

Exactly, I want to use linwrap but i don't want wordwrap...

I searched this

JTextPane textPane = new JTextPane();
JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
JScrollPane scrollPane = new JScrollPane( noWrapPanel );

When i use this code, Both linewrap and wordwrap are disabled....

I want to make use linewrap....

sorry my terrible English skill... I believe you can get what i mean please help me...

Was it helpful?

Solution

Here it is :

Link for it

// Override getScrollableTracksViewportWidth
// to preserve the full width of the text
public boolean getScrollableTracksViewportWidth() {
    Component parent = getParent();
    ComponentUI ui = getUI();

    return parent != null ? (ui.getPreferredSize(this).width <= parent
        .getSize().width) : true;
}

Other link

OTHER TIPS

Please try this.

JTextPane textPane;

public void someMethod() {  
    textPane = new JTextPane(new DefaultStyledDocument());
    textPane.setEditorKit(new ExtendedStyledEditorKit());
}


/** To enable no wrap to JTextPane **/
static class ExtendedStyledEditorKit extends StyledEditorKit {
    private static final long serialVersionUID = 1L;

    private static final ViewFactory styledEditorKitFactory = (new StyledEditorKit()).getViewFactory();

    private static final ViewFactory defaultFactory = new ExtendedStyledViewFactory();

    public Object clone() {
        return new ExtendedStyledEditorKit();
    }

    public ViewFactory getViewFactory() {
        return defaultFactory;
    }

    /* The extended view factory */
    static class ExtendedStyledViewFactory implements ViewFactory {
        public View create(Element elem) {
            String elementName = elem.getName();
            if (elementName != null) {
                if (elementName.equals(AbstractDocument.ParagraphElementName)) {
                    return new ExtendedParagraphView(elem);
                }
            }

            // Delegate others to StyledEditorKit
            return styledEditorKitFactory.create(elem);
        }
    }

}

static class ExtendedParagraphView extends ParagraphView {
    public ExtendedParagraphView(Element elem) {
        super(elem);
    }

    @Override
    public float getMinimumSpan(int axis) {
        return super.getPreferredSpan(axis);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top