JTextAreaに行カーソルを表示

編集者:Terai Atsuhiro~

作成日:2006-01-30
更新日:2022-10-07 (金) 15:03:11
  • 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

概要

JTextAreaのカーソルがある行全体にアンダーラインを引きます。

概要

JTextAreaのカーソルがある行全体にアンダーラインを引きます。

サンプルコード

#spanend
#spanadd
class LineCursorTextArea extends JTextArea {
#spanend
  private static final Color LINE_COLOR = Color.BLUE;
  private DefaultCaret caret;

#spandel
http://terai.xrea.jp/swing/linecursor/screenshot.png
#spanend
  @Override public void updateUI() {
    super.updateUI();
    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);
  }

#spandel
**サンプルコード [#aed50ce8]
#spanend
 class LineCursorTextArea extends JTextArea {
   public LineCursorTextArea() {
     super();
     Caret caret = new DefaultCaret() {
       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();
         }
       }
     };
     caret.setBlinkRate(getCaret().getBlinkRate());
     setCaret(caret);
   }
   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(SystemColor.activeCaption);
     g2.drawLine(i.left, y-1, getSize().width-i.left-i.right, y-1);
   }
   public static int getLineAtCaret(JTextComponent component) {
     int caretPosition = component.getCaretPosition();
     Element root = component.getDocument().getDefaultRootElement();
     return root.getElementIndex(caretPosition)+1;
   }
 }
  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    Insets i = getInsets();
    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();
  }

-[[サンプルを起動>http://terai.xrea.jp/swing/linecursor/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/linecursor/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/linecursor/src.zip]]
  // public static int getLineAtCaret(JTextComponent component) {
  //   int caretPosition = component.getCaretPosition();
  //   Element root = component.getDocument().getDefaultRootElement();
  //   return root.getElementIndex(caretPosition) + 1;
  // }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin

解説

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

解説

JTextArea#paintComponentメソッドをオーバーライドして、カーソルがある行にアンダーラインを引いています。 キャレットの移動に対応するため、DefaultCaret#damageメソッドを変更して、描画に使われる領域を描画し直しています。

参考リンク

参考リンク

コメント

  • JEditPaneではgetRowHeight()が使えないようなので、Java低速GUI Swing 3を参考にしてg2.getFontMetrics().getHeight()にしました。 -- terai

コメント