Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Vertical Scrolling and Horizontal Wrapping in a JEditorPane

796262Dec 15 2009 — edited Dec 16 2009
Hey guys,

Some of you may recall my [post |http://forums.sun.com/thread.jspa?threadID=5418688] from a couple weeks back. In it, I learned how to force (or appear to force) the text of a JEditorPane to grow from the bottom-up instead of top-down (picture the difference between how a chat window grows to how a word document grows). The solution (which solves my original problem perfectly) involves simply placing the JEditorPane into the South region of a JPanel.

That was all fine and dandy, until I tried putting long lines of text into the JEditorPane. It scrolled vertically when it should have, but the text simply ran off the right side of the JEditorPane instead of wrapping (or showing a horizontal scrollbar).

After a gross amount of googling, I finally came across this old [post |http://forums.sun.com/thread.jspa?threadID=5318664] that seems to be related to my problem here. It hinted that I should set the width of the JPanel to match the width of the JScrollPane's Viewport. While that does indeed fix the problem with horizontal wrapping, it now creates a problem with the vertical scrolling (which works fine without the fix for horizontal wrapping)!

I've tried setting the preferred height of the JPanel to just about everything I can think of: the height of the viewport, extremely large numbers, negative numbers, zero, itself, the height of the JEditorPane. But they all do the same thing: no vertical scrollbar ever pops up, even when the text is obviously too long to fit in the window.

Here's an SSCCE demonstrating what I'm talking about:
import javax.swing.*;
import java.awt.*;

public class EditorPaneTest {
    public EditorPaneTest() {
    	JFrame frame = new JFrame("EditorPane Test");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	JEditorPane textComponent = new JEditorPane();
    	textComponent.setContentType("text/html");
    	
    	String text = 
    		"This sentence should wrap and not cause the horiztonal scroll bar to display.<br/>"
    		+ "<br/>This<br/>sentence<br/>should<br/>cause<br/>"
    		+ "the<br/>vertical<br/>scroll<br/>bar<br/>to<br/>display.<br/>";
    		
    	
    	textComponent.setText(text);
    	
    	JPanel panel = new JPanel(new BorderLayout());
    	//the purpose of putting the JEditorPane in a JPanel
    	//is to force it to the bottom with BorderLayout
    	panel.add(textComponent, BorderLayout.SOUTH);
    	
    	JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    	
    	//this line enables wrapping
    	//but disables vertical scrolling
    	//(comment it out to switch)
    	panel.setPreferredSize(new Dimension(scrollPane.getViewport().getWidth(), scrollPane.getViewport().getHeight()));
    	
    	frame.add(scrollPane);
    	frame.setSize(100, 200);
    	frame.setVisible(true); 
    }
    
    public static void main(String[] args) {
        new EditorPaneTest();
    }
}
As always, any pointers / suggestions / criticisms you can give me are greatly appreciated.

Thanks again,

Kevin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2010
Added on Dec 15 2009
5 comments
1,355 views