JTextPane line wrapping

20,990

Solution 1

See No Wrap Text Pane. Here's the code included from the link.

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

Solution 2

The No Wrap Text Pane also provides an alternative solution that doesn't require wrapping the JTextPane in a JPanel, instead it overrides getScrollableTracksViewportWidth(). I prefer that solution, but it didn't quite work for me - I noticed that wrapping still occurs if the viewport becomes narrower than the minimum width of the JTextPane.

I found that JEditorPane is overriding getPreferredSize() to try and 'fix' things when the viewport is too narrow by returning the minimum width instead of the preferred width. This can be resolved by overriding getPreferredSize() again to say 'no, really - we always want the actual preferred size':

public class NoWrapJTextPane extends JTextPane {
    @Override
    public boolean getScrollableTracksViewportWidth() {
        // Only track viewport width when the viewport is wider than the preferred width
        return getUI().getPreferredSize(this).width 
            <= getParent().getSize().width;
    };

    @Override
    public Dimension getPreferredSize() {
        // Avoid substituting the minimum width for the preferred width when the viewport is too narrow
        return getUI().getPreferredSize(this);
    };
}
Share:
20,990
Jeffrey
Author by

Jeffrey

Updated on December 15, 2020

Comments

  • Jeffrey
    Jeffrey over 3 years

    Unlike JTextArea, JTextPane has no option to turn line wrapping off. I found one solution to turning off line wrapping in JTextPanes, but it seems too verbose for such a simple problem. Is there a better way to do this?

  • Jeffrey
    Jeffrey almost 13 years
    The only thing wrapping the JTextPane in a JPanel did was disable the vertical scrollbar.
  • camickr
    camickr almost 13 years
    @Jeffrey, works fine for me using JDK6_7 (and earlier versions) on XP. I don't think I'd go to all the trouble of creating a blog entry if it didn't work. Post your SSCCE that shows how you tested it.
  • camickr
    camickr almost 13 years
    @Jeffrey, NEVER set the preferred size of a component you add to a scrollpane. If you do then the scrollpane can't do its job. Instead set the preferred size of the scrollpane.
  • Jeffrey
    Jeffrey almost 13 years
    Ah, didn't know that. Updated the SSCCE and image. Now I have vertical scrolling, but it's still line wrapping.
  • camickr
    camickr almost 13 years
    @Jeffrey, you need to add the panel to the scrollpane.
  • Jeffrey
    Jeffrey almost 13 years
    The panel is passed to JScrollPane.setViewportView(Component) when you call the constructor. Using JScrollPane.add(Component) doesn't do what you expect it to.
  • camickr
    camickr almost 13 years
    @Jeffrey, I know how a scrollpane works (I always set the viewport via the constructor). If you don't understand what I'm suggesting then please copy the 4 lines of code from the blog that demonstrates how to do this. That is what you should have done from the start!
  • Jeffrey
    Jeffrey almost 13 years
    What's the difference between my code and the code on the blog post?
  • camickr
    camickr almost 13 years
    Last comment. This is taking too much time. All you have to do is copy the code and test it! Thats why I included the code. You are creating the JScrollPane with the "textpane". The blog creates the JScrollPane with the "noWrapPanel".
  • Jeffrey
    Jeffrey almost 13 years
    Oopsies. I tested the posted code, but changed it to new JScrollPane(panel) in my IDE so I didn't see anything wrong with the code. Sorry for wasting your time.