Swing/LineSpacing のバックアップの現在との差分(No.16)
- category: swing folder: LineSpacing title: JEditorPaneやJTextPaneに行間を設定する tags: [JEditorPane, JTextPane, StyledEditorKit] author: aterai pubdate: 2009-11-02T12:51:40+09:00 description: JEditorPaneやJTextPaneに行間を設定します。 image:
概要
JEditorPane
やJTextPane
に行間を設定します。
Screenshot
Advertisement
サンプルコード
class BottomInsetEditorKit extends StyledEditorKit {
@Override public ViewFactory getViewFactory() {
return new ViewFactory() {
@Override public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new javax.swing.text.ParagraphView(elem) {
@Override protected short getBottomInset() {
return 5;
}
};
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
return new LabelView(elem);
}
};
}
}
View in GitHub: Java, Kotlin解説
- 上:
StyleConstants.setLineSpacing
で、行間を指定したAttributeSet
を作成し、JTextPane#setParagraphAttributes
で設定 - 上:
StyleConstants.setLineSpacing
で行間を指定したAttributeSet
を作成し、JTextPane#setParagraphAttributes
で設定- フォントサイズ相対の行間になる
SimpleAttributeSet a = new SimpleAttributeSet();
StyleConstants.setLineSpacing(a, .5f);
#spandel
//StyleConstants.setSpaceAbove(a, 5f);
#spanend
#spandel
//StyleConstants.setSpaceBelow(a, 5f);
#spanend
#spandel
//StyleConstants.setLeftIndent(a, 5f);
#spanend
#spandel
//StyleConstants.setRightIndent(a, 5f);
#spanend
#spanadd
// StyleConstants.setSpaceAbove(a, 5f);
#spanend
#spanadd
// StyleConstants.setSpaceBelow(a, 5f);
#spanend
#spanadd
// StyleConstants.setLeftIndent(a, 5f);
#spanend
#spanadd
// StyleConstants.setRightIndent(a, 5f);
#spanend
editor1.setParagraphAttributes(a, true);
#spandel
setDummyText(editor1);
#spanend
#spanadd
setSampleText(editor1);
#spanend
- 下:
ParagraphView#getBottomInset
をオーバーライドして、固定の行間をピクセルで指定- フォントサイズに関係なく、アキ
5px
- フォントサイズに関係なく、アキ
- 下:
ParagraphView#getBottomInset
をオーバーライドして固定の行間をピクセルで指定- フォントサイズに関係なくアキ
5px
- フォントサイズに関係なくアキ
- スタイルシートで
line-height
を指定しても反映されない-
line-height
は、モデル化されているが、現在は描画されない -
line-height
はモデル化されているが、現在は描画されない - 対応しているCSSプロパティ一覧 - CSS (Java Platform SE 8)
-
- ブロックレベルで
1
行だけ固定の行間を指定したい場合はmargin-bottom
が使用可能StyleSheet styleSheet = new StyleSheet(); styleSheet.addRule("body {font-size: 24pt; line-height: 2.0}"); styleSheet.addRule(".test {margin-bottom: 2pt; padding-bottom: 1px; }"); #spandel //XXX: styleSheet.addRule("span {color: white; display:inline-block; margin-bottom: 10pt;}"); #spanend #spanadd // XXX: styleSheet.addRule("span {color: white; display:inline-block; margin-bottom: 10pt;}"); #spanend HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); htmlEditorKit.setStyleSheet(styleSheet); editor1.setEditorKit(htmlEditorKit); editor1.setText("<html><body><div class='test'>12<br />a<br />n<font size='32'>123<br />sd</font></div></body></html>");
参考リンク
- JTextPane#setParagraphAttributes(...) (Java Platform SE 8)
- CompositeView#getBottomInset() (Java Platform SE 8)