• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JEditorPaneやJTextPaneに行間を設定する
#navi(../)
RIGHT:Posted by [[terai]] at 2009-11-02
RIGHT:Posted by [[aterai]] at 2009-11-02
*JEditorPaneやJTextPaneに行間を設定する [#u308ecd7]
JEditorPaneやJTextPaneに行間を設定します。

-&jnlp;
-&jar;
-&zip;

#screenshot
//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTPYZn_u9I/AAAAAAAAAd4/5-1ThpWwM5U/s800/LineSpacing.png)

**サンプルコード [#x743f159]
#code{{
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);
      }
    };
  }
}
}}

**解説 [#oa458f2f]
-上: StyleConstants.setLineSpacingで、行間を指定したAttributeSetを作成し、JTextPane#setParagraphAttributesで設定しています。
-- フォントサイズ相対の行間になる
#code{{
SimpleAttributeSet a = new SimpleAttributeSet();
StyleConstants.setLineSpacing(a, .5f);
//StyleConstants.setSpaceAbove(a, 5.0f);
//StyleConstants.setSpaceBelow(a, 5.0f);
//StyleConstants.setLeftIndent(a, 5.0f);
//StyleConstants.setRightIndent(a, 5.0f);
editor1.setParagraphAttributes(a, true);
setDummyText(editor1);
}}

-下: ParagraphView#getBottomInsetをオーバーライドして、固定の行間をピクセルで指定しています。
-- フォントサイズに関係なく、アキ5px

----
スタイルシートでline-heightを指定しても反映されない?
#code{{
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: 24pt; line-height: 2.0}"); //XXX
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
editor1.setEditorKit(htmlEditorKit);
editor1.setText("<html><body>12<br />a<br />n<font size='32'>123<br />sd</font></body></html>");
}}

//**参考リンク
**コメント [#q7d6c5ab]
- この間も俊太郎の詩をお  http://www.stlouisbusinesslist.com/business/5021837.htm?info=viagra viagra   %OO   -- [[viagra]] &new{2010-03-08 (月) 12:30:38};

#comment