Swing/ParagraphMark のバックアップの現在との差分(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ParagraphMark へ行く。
- 1 (2007-09-14 (金) 21:05:17)
- 2 (2007-09-18 (火) 11:52:51)
- 3 (2007-09-21 (金) 17:04:17)
- 4 (2008-05-09 (金) 19:25:37)
- 5 (2011-06-23 (木) 19:02:58)
- 6 (2012-08-02 (木) 21:12:37)
- 7 (2013-02-05 (火) 17:25:09)
- 8 (2013-08-30 (金) 01:33:35)
- 9 (2014-10-11 (土) 16:47:58)
- 10 (2014-11-19 (水) 01:31:21)
- 11 (2014-11-25 (火) 03:03:31)
- 12 (2015-12-22 (火) 00:34:00)
- 13 (2016-09-15 (木) 16:31:48)
- 14 (2017-10-26 (木) 10:56:57)
- 15 (2019-04-10 (水) 13:46:47)
- 16 (2021-01-19 (火) 19:31:40)
- 17 (2023-07-12 (水) 17:12:54)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JEditorPaneで改行を表示 #navi(../) *JEditorPaneで改行を表示 [#l76cb974] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2007-06-11~ 更新日:&lastmod; --- category: swing folder: ParagraphMark title: JEditorPaneで改行を表示 tags: [JEditorPane, StyledEditorKit] author: aterai pubdate: 2007-06-11T17:28:09+09:00 description: JEditorPaneのパラグラフ終了位置に改行を意味する図形を追加表示します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTQ3cf9HLI/AAAAAAAAAgQ/gIbt2d-Hz7k/s800/ParagraphMark.png --- * 概要 [#summary] `JEditorPane`のパラグラフ終了位置に改行を意味する図形を追加表示します。[https://community.oracle.com/thread/1374478 Swing - JTextPane View Problem]から、ソースコードの大部分を引用しています。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTQ3cf9HLI/AAAAAAAAAgQ/gIbt2d-Hz7k/s800/ParagraphMark.png) **概要 [#r217a724] JEditorPaneで改行記号を表示します。[[JTextPane View Problem>http://forum.java.sun.com/thread.jspa?threadID=5122980]]から、ソースコードの大部分を引用しています。 #screenshot **サンプルコード [#r8bfcb2e] #code{{ * サンプルコード [#sourcecode] #code(link){{ class MyEditorKit extends StyledEditorKit { public ViewFactory getViewFactory() { @Override public ViewFactory getViewFactory() { return new MyViewFactory(); } } class MyViewFactory implements ViewFactory { public View create(Element elem) { @Override public View create(Element elem) { String kind = elem.getName(); if(kind!=null) { if(kind.equals(AbstractDocument.ContentElementName)) { if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new LabelView(elem); }else if(kind.equals(AbstractDocument.ParagraphElementName)) { return new MyParagraphView(elem); }else if(kind.equals(AbstractDocument.SectionElementName)) { } else if (kind.equals(AbstractDocument.ParagraphElementName)) { return new ParagraphWithEopmView(elem); } else if (kind.equals(AbstractDocument.SectionElementName)) { return new BoxView(elem, View.Y_AXIS); }else if(kind.equals(StyleConstants.ComponentElementName)) { } else if (kind.equals(StyleConstants.ComponentElementName)) { return new ComponentView(elem); }else if(kind.equals(StyleConstants.IconElementName)) { } else if (kind.equals(StyleConstants.IconElementName)) { return new IconView(elem); } } return new LabelView(elem); } } class MyParagraphView extends ParagraphView { class ParagraphWithEopmView extends ParagraphView { private static final Color pc = new Color(120, 130, 110); public MyParagraphView(Element elem) { public ParagraphWithEopmView(Element elem) { super(elem); } public void paint(Graphics g, Shape allocation) { @Override public void paint(Graphics g, Shape allocation) { super.paint(g, allocation); paintCustomParagraph(g, allocation); } public void paintCustomParagraph(Graphics g, Shape a) { private void paintCustomParagraph(Graphics g, Shape a) { try { Shape paragraph = modelToView(getEndOffset(), a, Position.Bias.Backward); int y = a.getBounds().y; int x = (paragraph==null)?a.getBounds().x:paragraph.getBounds().x; int z = a.getBounds().height / 2; Color old = g.getColor(); g.setColor(pc); g.drawLine(x+1, y+z, x+1, y+a.getBounds().height-4); g.drawLine(x+2, y+z, x+2, y+a.getBounds().height-5); g.drawLine(x+3, y+a.getBounds().height-6, x+3, y+a.getBounds().height-6); g.setColor(old); }catch(Exception e) { e.printStackTrace(); } Shape paragraph = modelToView( getEndOffset(), a, Position.Bias.Backward); Rectangle r = (paragraph == null) ? a.getBounds() : paragraph.getBounds(); int x = r.x; int y = r.y; int h = r.height; Graphics2D g2 = (Graphics2D) g.create(); g2.setColor(MARK_COLOR); // paragraph mark g2.drawLine(x + 1, y + h / 2, x + 1, y + h - 4); g2.drawLine(x + 2, y + h / 2, x + 2, y + h - 5); g2.drawLine(x + 3, y + h - 6, x + 3, y + h - 6); g2.dispose(); } catch (Exception e) { e.printStackTrace(); } } } }} -&jnlp; -&jar; -&zip; **解説 [#y0783bd1] StyledEditorKitを継承するEditorKitを作成し、これをJEditorPane#setEditorKitメソッドで、JEditorPaneに設定しています。 * 解説 [#explanation] - `StyledEditorKit`を継承する`EditorKit`を作成し、これを`JEditorPane#setEditorKit`メソッドで`JEditorPane`に設定 - この`EditorKit`は`Element`がパラグラフ(`AbstractDocument.ParagraphElementName`)の場合、改行記号を末尾に追加で描画する`View`を返す`ViewFactory`を生成 このEditorKitは、Elementが段落(AbstractDocument.ParagraphElementName)の場合、独自の改行記号を追加で描画するViewを返すViewFactoryを生成しています。 * 参考リンク [#reference] - [https://community.oracle.com/thread/1374478 Swing - JTextPane View Problem] - [[JTextPaneで全角スペースやタブを可視化>Swing/WhitespaceMark]] **参考リンク [#vccc314a] -[[JTextPane View Problem>http://forum.java.sun.com/thread.jspa?threadID=5122980]] * コメント [#comment] #comment - 行の折り返しが発生すると、改行記号が縦長になるバグを修正。 -- &user(aterai); &new{2007-09-21 (金) 17:05:03}; **コメント [#tb94cc8f] #comment