• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaに行ハイライトカーソルを表示
#navi(../)
RIGHT:Posted by &author(aterai); at 2006-07-24
*JTextAreaに行ハイライトカーソルを表示 [#b84f9cac]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-07-24~
更新日:&lastmod;
JTextAreaのカーソルがある行をハイライト表示します。

#contents
-&jnlp;
-&jar;
-&zip;

**概要 [#x8bd671d]
JTextAreaのカーソルがある行をハイライト表示します。
//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTPQ5j7_JI/AAAAAAAAAds/kbet-1O8x-A/s800/LineHighlighter.png)

#screenshot

**サンプルコード [#vabb7f2d]
#code{{
#code(link){{
class HighlightCursorTextArea extends JTextArea {
  private static final Color linecolor = new Color(250,250,220);
  private final DefaultCaret caret;
  public HighlightCursorTextArea() {
    super();
    setOpaque(false);
    caret = new DefaultCaret() {
      @Override
      protected synchronized void damage(Rectangle r) {
        if(r!=null) {
          JTextComponent c = getComponent();
          x = 0;
          y = r.y;
          width  = c.getSize().width;
          height = r.height;
          c.repaint();
        }
      }
    };
    caret.setBlinkRate(getCaret().getBlinkRate());
    setCaret(caret);
  }
  @Override
  protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Insets i = getInsets();
    int h = caret.height;
    int y = caret.y;
    g2.setPaint(linecolor);
    g2.fillRect(i.left, y, getSize().width-i.left-i.right, h);
    super.paintComponent(g);
  }
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#jb96e960]
ほぼ、[[JTextAreaに行カーソルを表示>Swing/LineCursor]]とやっていることは同じです。

違うのは、以下の点になります。
-ViewPortの色をscroll.getViewport().setBackground(Color.WHITE)にする
-JTextAreaをsetOpaque(false)と透明にする
-JTextAreaのpaintComponent(Graphics g)をオーバーライドするとき、カーソルのある行を塗りつぶしてからsuper.paintComponent(g)する

----
[[Swing - Stretching background colour across whole JTextPane for one line of text>http://forum.java.sun.com/thread.jspa?threadID=5275222]] のように、BasicTextPaneUI#paintBackground をオーバーライドする方法(こちらの方がシンプルで美しいかも)もあります。
[http://forums.sun.com/thread.jspa?threadID=5275222 Swing - Stretching background colour across whole JTextPane for one line of text] の Darryl.Burke さんのコード(以下に部分コピー)のように、BasicTextPaneUI#paintBackground をオーバーライドする方法(こちらの方がシンプルで美しいかも)もあります。

#code{{
//JTextPane textPane = new JTextPane();
//textPane.setUI(new LineHighlightTextPaneUI(textPane));
class LineHighlightTextPaneUI extends BasicTextPaneUI {
   private final JTextPane tc;
   public LineHighlightTextPaneUI(JTextPane t) {
      tc = t;
      tc.addCaretListener(new CaretListener() {
         public void caretUpdate(CaretEvent e) {
            tc.repaint();
         }
      });
   }
   @Override
   public void paintBackground(Graphics g) {
      super.paintBackground(g);
      try {
         Rectangle rect = modelToView(tc, tc.getCaretPosition());
         int y = rect.y;
         int h = rect.height;
         g.setColor(Color.YELLOW);
         g.fillRect(0, y, tc.getWidth(), h);
      }catch(BadLocationException ex) {
         ex.printStackTrace();
      }
   }
}
}}

これらの方法なら、JTextEditorやJTextPaneで行の高さが異なる場合でも、うまくハイライトできるようです。
//#screenshot(,screenshot1.png)
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTPTaywxYI/AAAAAAAAAdw/RIlfRHiC-JY/s800/LineHighlighter1.png)

**参考リンク [#y021b082]
-[[JTextAreaに行カーソルを表示>Swing/LineCursor]]
-[[[[Swing - Stretching background colour across whole JTextPane for one line of text>http://forum.java.sun.com/thread.jspa?threadID=5275222]]]]
-[http://forums.sun.com/thread.jspa?threadID=5275222 Swing - Stretching background colour across whole JTextPane for one line of text]

**コメント [#v72b7b28]
- 行の折り返しに対応しました。 -- [[terai]] &new{2008-03-17 (月) 16:22:56};
- 行の折り返しに対応しました。 -- [[aterai]] &new{2008-03-17 (月) 16:22:56};

#comment