• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaに行カーソルを表示
#navi(../)
*JTextAreaに行カーソルを表示 [#x5c3f9a7]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-01-30~
更新日:&lastmod;
Posted by [[terai]] at 2006-01-30

#contents

**概要 [#sb8c4958]
JTextAreaのカーソルがある行全体にアンダーラインを引きます。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#aed50ce8]
#code{{
class LineCursorTextArea extends JTextArea {
  private static final Color cfc = Color.BLUE;
  private final DefaultCaret caret;
  public LineCursorTextArea() {
    super();
    Caret caret = new DefaultCaret() {
    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;
          repaint();
          c.repaint();
        }
      }
    };
    caret.setBlinkRate(getCaret().getBlinkRate());
    setCaret(caret);
  }
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    Insets i = getInsets();
    int y = g2.getFontMetrics().getHeight()*getLineAtCaret(this)+i.top;
    g2.setPaint(Color.BLUE);
    g2.drawLine(i.left, y-1, getSize().width-i.left-i.right, y-1);
    //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);
  }
  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;
//   }
}
}}
-&jnlp;
-&jar;
-&zip;

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

キャレットの移動に対応するため、DefaultCaret#damageメソッドを変更して、描画に使われる領域を描画し直しています。

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

**参考リンク [#j7373295]
-[[Line Number in JTextPane>http://forum.java.sun.com/thread.jspa?threadID=613385]]
-[[Line highlighting problem in presence of text highlighting!>http://forum.java.sun.com/thread.jspa?threadID=768960]]
-[[Swing - Line Number in JTextPane>http://forums.sun.com/thread.jspa?threadID=613385]]
-[[Swing - Line highlighting problem in presence of text highlighting!>http://forums.sun.com/thread.jspa?threadID=768960]]
-[[Java低速GUI Swing 3>http://pc8.2ch.net/test/read.cgi/tech/1121700954/813-818?&nofirst=true]]
-[[Highlighting Current Line >http://www.jroller.com/page/santhosh/20050601?catname=%2FSwing]]
-[[JTextAreaに行ハイライトカーソルを表示>Swing/LineHighlighter]]

**コメント [#v0898907]
- JEditorPaneではgetRowHeight()が使えないようなので、[[Java低速GUI Swing 3>http://pc8.2ch.net/test/read.cgi/tech/1121700954/813-818?&nofirst=true]]を参考にしてg2.getFontMetrics().getHeight()にしました。 -- [[terai]] &new{2006-01-30 (月) 12:24:40};
- %%JEditorPaneではgetRowHeight()が使えないようなので、[[Java低速GUI Swing 3>http://pc8.2ch.net/test/read.cgi/tech/1121700954/813-818?&nofirst=true]]を参考にしてg2.getFontMetrics().getHeight()にしました。%% -- [[terai]] &new{2006-01-30 (月) 12:24:40};
-- Caret の高さを使用するように変更しました。 -- [[terai]] &new{2008-03-17 (月) 16:54:50};

#comment