• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: WhitespaceMark
title: JTextPaneで全角スペースやタブを可視化
tags: [JTextPane, LabelView]
author: aterai
pubdate: 2007-09-17T14:41:19+09:00
description: JTextPaneに表示した文字列中の全角スペースやタブを可視化します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWpb1ogMI/AAAAAAAAApk/3IWJ2qvvECo/s800/WhitespaceMark.png
---
* 概要 [#vbe52881]
* 概要 [#summary]
`JTextPane`に表示した文字列中の全角スペースやタブを可視化します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWpb1ogMI/AAAAAAAAApk/3IWJ2qvvECo/s800/WhitespaceMark.png)

* サンプルコード [#f57a2d79]
* サンプルコード [#sourcecode]
#code(link){{
class WhitespaceLabelView extends LabelView {
  private static final Color pc = new Color(130, 140, 120);
  private static final BasicStroke dashed = new BasicStroke(1f, BasicStroke.CAP_BUTT,
       BasicStroke.JOIN_MITER, 10f, new float[] {1f}, 0f);
  public WhitespaceLabelView(Element elem) {
  private static final String IDEOGRAPHIC_SPACE = "\u3000";
  private static final Color MARK_COLOR = new Color(130, 140, 120);
  private static final BasicStroke DASHED = new BasicStroke(
      1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, new float[] {1f}, 0f);
  protected WhitespaceLabelView(Element elem) {
    super(elem);
  }

  @Override public void paint(Graphics g, Shape a) {
    super.paint(g, a);
    Graphics2D g2 = (Graphics2D) g.create();
    Stroke stroke = g2.getStroke();
    Rectangle alloc = (a instanceof Rectangle) ? (Rectangle) a : a.getBounds();
    Rectangle alloc = a instanceof Rectangle ? (Rectangle) a : a.getBounds();
    FontMetrics fontMetrics = g.getFontMetrics();
    int spaceWidth = fontMetrics.stringWidth(" ");
    int sumOfTabs  = 0;
    int spaceWidth = fontMetrics.stringWidth(IDEOGRAPHIC_SPACE);
    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);
      if (IDEOGRAPHIC_SPACE.equals(s)) {
        g2.setStroke(DASHED);
        g2.setPaint(MARK_COLOR);
        g2.drawLine(sx + 1, sy - 1, sx + spaceWidth - 2, sy - 1);
        g2.drawLine(sx + 2, sy,     sx + spaceWidth - 2, sy);
        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.setPaint(MARK_COLOR);
        g2.drawLine(sx + 2, sy, sx + 2 + 2, sy);
        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 - 2, sx + 2, sy - 2);
        g2.setStroke(DASHED);
        g2.drawLine(sx + 2, sy, sx + tabWidth - 2, sy);
        sumOfTabs += tabWidth;
      }
      g2.setStroke(stroke);
    }
    g2.dispose();
  }
}
}}

* 解説 [#wfb05641]
上記のサンプルでは、`LabelView`を継承し、`LabelView#paint`メソッドをオーバーライドして全角スペースやタブの場合だけ、それぞれの図形を描画しています。
* 解説 [#explanation]
上記のサンプルでは、`LabelView`を継承し、`LabelView#paint`メソッドをオーバーライドして全角スペースやタブの場合だけそれぞれの図形を描画しています。

- 注: タブの図形は、`xyzzy`風だが、ドットの数は面倒なので適当
- タブ表示図形のドット数は適当(`4`タブの場合に`4`点表示している訳ではない)
- `LabelView`を使用する`EditorKit(ViewFactory)`の作成方法は[[JEditorPaneで改行を表示>Swing/ParagraphMark]]と同様

この`LabelView`を使用する`EditorKit(ViewFactory)`の作成は、[[JEditorPaneで改行を表示>Swing/ParagraphMark]]と同様になっています。

* 参考リンク [#ja0a61a4]
* 参考リンク [#reference]
- [https://community.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]]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/LabelView.html LabelView (Java Platform SE 8)]

* コメント [#j5c6e373]
* コメント [#comment]
#comment
#comment