设置StyledDocument(JTextPane)的主体宽度

时间:2015-05-07 09:08:50

标签: java swing jtextpane

我可以这样做,我的富文本将有固定的宽度

JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><body style='width:100px'>Some text</body></html>"); 

但我直接使用Document,没有HTML。我该如何设置宽度?

DefaultStyledDocument doc = (DefaultStyledDocument) pane.getStyledDocument();
//??? (set width)

2 个答案:

答案 0 :(得分:0)

宽度不是模型的一部分(文档)。应该有一个View来处理Document中的width属性并相应地设置宽度。

您应该在EditorKit的ViewFactory中替换根视图(SectionView),以返回宽度取自Document元素的自定义视图。

参见例如链接 http://java-sl.com/Pagination_In_JEditorPane.html

它描述了如何向EditorKit添加分页。您可以使用代码作为示例并仅修复宽度。

Extend EditorKit.
Replace default ViewFactory with yours
Replace root view (SectionView)
Replace the view's getPreferredSpan(int axis) for X axis to use the width from the Document

答案 1 :(得分:0)

除了Document是StyledDocument之外,还有两个级别:段落和字符。在这种情况下,可以使用HTMLDocument并使用CSS样式表(body { width: 14cm; })构建它,或者适当地更改CSS样式表。

相关问题