-1

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 --*/
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Humble Potato II
  • 161
  • 1
  • 1
  • 12
  • Do you find this? I believe it contains the answer. http://stackoverflow.com/questions/7036543/how-is-word-wrapping-implemented-in-jtextpane-and-how-do-i-make-it-wrap-a-strin – ControlAltDel Jan 09 '17 at 20:08
  • @ControlAltDel Nope. Okay, I think I need an example of this so I can see where the code will be placed. – Humble Potato II Jan 09 '17 at 20:14
  • *"I think I need an example.."* I think you need a tutor or a help desk. This is not the place for people unwilling or unable to try implementing advice. – Andrew Thompson Jan 09 '17 at 22:29
  • @AndrewThompson "I think I need a tutor"? I replied to the comment with **Nope**. The question referred which asked 5 years ago was kinda related to mine, but the question is pointing to "how does the implementation works", while my question is "how to implement it". And who says I'm asking for a code to someone else? I've clearly said that "I think I need an example", so I've searched again looking for an example somewhere. Never mind. Thank you for your down vote. – Humble Potato II Jan 10 '17 at 00:08

1 Answers1

1

Check out No Wrap Text Pane for potentially simpler solutions.

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

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hmmm, I was actually using the NetBeans designer so I think setting a `BorderLayout()` will just mess the UI... But I'll also try it. – Humble Potato II Jan 10 '17 at 00:12
  • So I tried the `public boolean getScrollableTracksViewportWidth()` overriding the `JTextPane textPane = new JTextPane()`. It works but my program's layout is just `pack()`ed not `fixed width` so it is messed up. By the way, much simpler solution to this kind of problem. Thank you :) – Humble Potato II Jan 10 '17 at 00:37