No Wrap Text Pane also provides an alternative solution that does not require wrapping a JTextPane in a JPanel ; instead, it overrides getScrollableTracksViewportWidth() . I prefer this solution, but it didn’t work for me at all - I noticed that packaging still occurs if the viewport becomes already the minimum width of the JTextPane .
I found that JEditorPane overrides getPreferredSize() to try to “fix” a situation where the viewport is too narrow, 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() {
Kevin k
source share