Swing/WhitespaceMark のバックアップソース(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/WhitespaceMark へ行く。
- 1 (2007-09-17 (月) 14:41:19)
- 2 (2007-09-18 (火) 11:51:45)
- 3 (2013-01-31 (木) 23:01:49)
- 4 (2013-05-10 (金) 10:49:06)
- 5 (2013-09-04 (水) 00:11:30)
- 6 (2013-10-17 (木) 04:28:42)
- 7 (2013-11-05 (火) 19:24:50)
- 8 (2014-11-22 (土) 03:59:58)
- 9 (2014-11-25 (火) 03:03:31)
- 10 (2015-01-15 (木) 15:31:24)
- 11 (2016-02-05 (金) 16:38:14)
- 12 (2017-07-05 (水) 13:52:56)
- 13 (2018-07-05 (木) 16:00:37)
- 14 (2020-06-26 (金) 19:23:07)
- 15 (2021-12-01 (水) 08:33:37)
--- title: JTextPaneで全角スペースやタブを可視化 tags: [JTextPane, LabelView] author: aterai pubdate: 2007-09-17T14:41:19+09:00 description: JTextPaneに表示した文字列中の全角スペースやタブを可視化します。 --- * 概要 [#vbe52881] `JTextPane`に表示した文字列中の全角スペースやタブを可視化します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWpb1ogMI/AAAAAAAAApk/3IWJ2qvvECo/s800/WhitespaceMark.png) * サンプルコード [#f57a2d79] #code(link){{ class WhitespaceLabelView extends LabelView { private static final Color pc = new Color(130, 140, 120); private static final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {1.0f}, 0.0f); public WhitespaceLabelView(Element elem) { super(elem); } @Override public void paint(Graphics g, Shape a) { super.paint(g,a); Graphics2D g2 = (Graphics2D) g; Stroke stroke = g2.getStroke(); Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds(); FontMetrics fontMetrics = g.getFontMetrics(); int spaceWidth = fontMetrics.stringWidth(" "); int sumOfTabs = 0; String text = getText(getStartOffset(),getEndOffset()).toString(); for(int i=0;i<text.length();i++) { String s = text.substring(i,i+1); int previousStringWidth = fontMetrics.stringWidth(text.substring(0,i)) + sumOfTabs; int sx = alloc.x+previousStringWidth; int sy = alloc.y+alloc.height-fontMetrics.getDescent(); if(" ".equals(s)) { g2.setStroke(dashed); g2.setPaint(pc); g2.drawLine(sx+1, sy-1, sx+spaceWidth-2, sy-1); g2.drawLine(sx+2, sy, sx+spaceWidth-2, sy); }else if("\t".equals(s)) { int tabWidth = (int)getTabExpander().nextTabStop((float)sx, i)-sx; g2.setColor(pc); g2.drawLine(sx+2, sy-0, sx+2+2, sy-0); g2.drawLine(sx+2, sy-1, sx+2+1, sy-1); g2.drawLine(sx+2, sy-2, sx+2+0, sy-2); g2.setStroke(dashed); g2.drawLine(sx+2, sy, sx+tabWidth-2, sy); sumOfTabs+=tabWidth; } g2.setStroke(stroke); } } } }} * 解説 [#wfb05641] 上記のサンプルでは、`LabelView`を継承し、`LabelView#paint`メソッドをオーバーライドして全角スペースやタブの場合だけ、それぞれの図形を描画しています。 - 注: タブの図形は、`xyzzy`風だが、ドットの数は面倒なので適当 この`LabelView`を使用する`EditorKit(ViewFactory)`の作成は、[[JEditorPaneで改行を表示>Swing/ParagraphMark]]と同様になっています。 * 参考リンク [#ja0a61a4] - [https://forums.oracle.com/thread/1374478 Swing - JTextPane View Problem] - [http://web.archive.org/web/20120216035951/http://java.sun.com/products/jlf/ed1/dg/higo.htm Design Guidelines: Text Components] - [http://www.javafaq.nu/java-book-30.html Swing Chapter 19. (Advanced topics) Inside Text Components. Easy for reading, Click here!] - [[JEditorPaneで改行を表示>Swing/ParagraphMark]] - [[JTextPaneでタブサイズを設定>Swing/TabSize]] * コメント [#j5c6e373] #comment #comment