JTextPane text wrap

I'm using a JTextPane inside a JScrollPane - a classical case:

JTextPane textPane = new JTextPane();

JScrollPane = new JScrollPane(textPane);

getContentPane().add(scrollPane);

So, this is how i add these two components on my JFrame.

When i type a lot of text into the JTextPane, a vertical scrollbar appears. However, if i have long lines in the JTextPane, a horizontal scrollbar doesn't appear. The lines get wrapped.

I want the horizontal scrollbar to appear.

I think there must be a line wrap option in the JTextPane attribute sets. However, i didn't succeed at finding them.

Or maybe i should do something with the JScrollPane?

I'd liked to get these few lines of code that solve the problem.

Thanks a lot.
LVL 2
MindoAsked:
Who is Participating?
 
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
This appears to work for JTextPane:

    JTextPane text = new JTextPane() {
            public boolean getScrollableTracksViewportWidth() {
                return (getSize().width < getParent().getSize().width);
            }
            public void setSize(Dimension d) {
                if (d.width < getParent().getSize().width) {
                    d.width = getParent().getSize().width;
                }
                super.setSize(d);
            }
        };
    JScrollPane scroll = new JScrollPane(text);

Best regards,
Jim Cakalic


0
 
MindoAuthor Commented:
Hmm, i added your code. The horizontal scrollbar doesn't appear though and the lines are still wrapped.
0
 
MindoAuthor Commented:
Doesn't work.
0
 
MindoAuthor Commented:
Sorry, i've messed up with my own code. Now i checked it once again. It works! Thanks, man.
0
All Courses

From novice to tech pro — start learning today.