Using no wrap implementation in JTextPane

249 views Asked by At

First, I am a newbie in Java programming. I want to disable the word-wraping in JTextPane cause there's no such options to do it unlike JTextArea. I got this one solution but I don't know how to implement it, like where should I place it in my code?

No Wrap implementation:

public class NoWrapParagraphView extends ParagraphView {

    public NoWrapParagraphView(Element elem) {
        super(elem);
    }

    @Override
    public void layout(int width, int height) {
        super.layout(Short.MAX_VALUE, height);
    }

    @Override
    public float getMinimumSpan(int axis) {
        return super.getPreferredSpan(axis);
    }
}

JTextArea and JScrollPane:

/*-- OTHER CODES --*/

JTextPane jTextPane = new JTextPane();
jTextPane.setContentType("text/html");
jTextPane.setEditable(false);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(jTextPane);

/*-- MORE CODES --*/
1

There are 1 answers

2
camickr On BEST ANSWER

Check out No Wrap Text Pane for potentially simpler solutions.

These solutions override the default Scrollable interface used by a JTextPane to prevent wrapping.