How to turn off JTextPane line wrapping?
Asked Answered
S

2

11

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?

Sumac answered 23/8, 2011 at 3:43 Comment(0)
Z
16

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 );
Zulema answered 23/8, 2011 at 3:53 Comment(10)
The only thing wrapping the JTextPane in a JPanel did was disable the vertical scrollbar.Sumac
@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.Zulema
@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.Zulema
Ah, didn't know that. Updated the SSCCE and image. Now I have vertical scrolling, but it's still line wrapping.Sumac
@Jeffrey, you need to add the panel to the scrollpane.Zulema
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.Sumac
@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!Zulema
What's the difference between my code and the code on the blog post?Sumac
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".Zulema
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.Sumac
S
4

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);
    };
}
Slumlord answered 14/12, 2017 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.