Unlike JTextArea
, JTextPane
has no option to turn line wrapping off. I found one solution to turning off line wrapping in JTextPane
s, but it seems too verbose for such a simple problem. Is there a better way to do this?
Asked
Active
Viewed 2.2k times
2 Answers
15
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 );
-
1The only thing wrapping the `JTextPane` in a `JPanel` did was disable the vertical scrollbar. – Jeffrey Aug 23 '11 at 04:09
-
@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 Aug 23 '11 at 05:56
-
1@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. – camickr Aug 23 '11 at 15:02
-
Ah, didn't know that. Updated the SSCCE and image. Now I have vertical scrolling, but it's still line wrapping. – Jeffrey Aug 23 '11 at 15:52
-
@Jeffrey, you need to add the panel to the scrollpane. – camickr Aug 23 '11 at 22:13
-
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. – Jeffrey Aug 23 '11 at 22:29
-
@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! – camickr Aug 24 '11 at 00:00
-
What's the difference between my code and the code on the blog post? – Jeffrey Aug 24 '11 at 00:15
-
1Last 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". – camickr Aug 24 '11 at 00:34
-
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. – Jeffrey Aug 24 '11 at 01:09
3
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);
};
}

Kevin K
- 9,344
- 3
- 37
- 62