Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

java - How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

How exactly is word-wrapping implemented in JTextPane?

I'm trying to understand exactly how it works so that I can modify the behavior. Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.

This article by Lapitsky says that LabelView.getBreakWeight() returns View.ExcellentBreakWeight for labels with spaces and View.GoodBreakWeight for labels without spaces (and the code in GlyphView.java seems to confirm this), so why doesn't it break? Is it somehow returning BadBreakWeight instead of GoodBreakWeight? Or is there some layout problem? Or is there a bug?

Here's some code (for your viewing pleasure):

//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);

Note that it still doesn't wrap if I take out the scroll pane and just use the text pane (it just gets clipped as it goes outside the window).


The javadocs for Swing don't seem to go into enough detail on how some objects (like JTextPane, View's, and related objects) work together. Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together? Is it just not publicly available? (Or am I the only one having trouble with things like this? Or is the insufficient documentation limited to things which aren't expected to be dealt with by a typical developer?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The link about custom wrap (forced wrap and no wrap). http://java-sl.com/wrap.html

The link about letter wrap http://java-sl.com/tip_html_letter_wrap.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...