• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: LineCursor
title: JTextAreaに行カーソルを表示
tags: [JTextArea, Caret, Highlighter]
author: aterai
pubdate: 2006-01-30T12:22:18+09:00
description: JTextAreaのカーソルがある行全体にアンダーラインを引きます。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTPL3eZj2I/AAAAAAAAAdk/KJTR3_NeAZE/s800/LineCursor.png
---
* 概要 [#x5c3f9a7]
* 概要 [#summary]
`JTextArea`のカーソルがある行全体にアンダーラインを引きます。

#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTPL3eZj2I/AAAAAAAAAdk/KJTR3_NeAZE/s800/LineCursor.png)

* サンプルコード [#aed50ce8]
* サンプルコード [#sourcecode]
#code(link){{
class LineCursorTextArea extends JTextArea {
  private static final Color cfc = Color.BLUE;
  private final DefaultCaret caret;
  public LineCursorTextArea() {
    super();
  private static final Color LINE_COLOR = Color.BLUE;
  private DefaultCaret caret;

  @Override public void updateUI() {
    super.updateUI();
    caret = new DefaultCaret() {
      @Override protected synchronized void damage(Rectangle r) {
        if(r!=null) {
        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) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Graphics2D g2 = (Graphics2D) g.create();
    Insets i = getInsets();
    //int y = g2.getFontMetrics().getHeight()*getLineAtCaret(this)+i.top;
    int y = caret.y+caret.height-1;
    g2.setPaint(cfc);
    g2.drawLine(i.left, y, getSize().width-i.left-i.right, y);
    int y = caret.y + caret.height - 1;
    g2.setPaint(LINE_COLOR);
    g2.drawLine(i.left, y, getSize().width - i.left - i.right, y);
    g2.dispose();
  }
//   public static int getLineAtCaret(JTextComponent component) {
//     int caretPosition = component.getCaretPosition();
//     Element root = component.getDocument().getDefaultRootElement();
//     return root.getElementIndex(caretPosition)+1;
//   }

  // public static int getLineAtCaret(JTextComponent component) {
  //   int caretPosition = component.getCaretPosition();
  //   Element root = component.getDocument().getDefaultRootElement();
  //   return root.getElementIndex(caretPosition) + 1;
  // }
}
}}

* 解説 [#b5ead0fc]
* 解説 [#explanation]
`JTextArea#paintComponent`メソッドをオーバーライドして、カーソルがある行にアンダーラインを引いています。

キャレットの移動に対応するため、`DefaultCaret#damage`メソッドを変更して、描画に使われる領域を描画し直しています。
- `Caret`の移動に対応するため`DefaultCaret#damage(Rectangle)`メソッドをオーバーライドして変更された領域を再描画
- [https://github.com/santhosh-tekuri/MyBlog/tree/master/CurrentLineHighlighter MyBlog/CurrentLineHighlighter at master · santhosh-tekuri/MyBlog · GitHub]のように`Highlighter`を使用する方法もある

[http://www.jroller.com/page/santhosh/20050601?catname=%2FSwing Highlighting Current Line ]のように、`Highlighter`を使っても同様のことができるようです。

* 参考リンク [#j7373295]
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1393939 Swing - Line Number in JTextPane]
- [https://community.oracle.com/thread/1377129 Swing - Line highlighting problem in presence of text highlighting!]
- [http://www.jroller.com/page/santhosh/20050601?catname=%2FSwing Highlighting Current Line ]
- [https://github.com/santhosh-tekuri/MyBlog/tree/master/CurrentLineHighlighter MyBlog/CurrentLineHighlighter at master · santhosh-tekuri/MyBlog · GitHub]
- [[JTextAreaに行ハイライトカーソルを表示>Swing/LineHighlighter]]

* コメント [#v0898907]
* コメント [#comment]
#comment
- `Caret`の高さを使用するように変更しました。 -- &user(aterai); &new{2008-03-17 (月) 16:54:50};
- `Caret`の高さを使用するよう変更 -- &user(aterai); &new{2008-03-17 (月) 16:54:50};

#comment