Swing/LineCursor のバックアップ差分(No.20)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/LineCursor へ行く。
- 1 (2006-01-30 (月) 12:22:18)
- 2 (2006-01-30 (月) 13:45:39)
- 3 (2006-02-19 (日) 15:55:56)
- 4 (2006-02-27 (月) 16:07:47)
- 5 (2006-03-30 (木) 15:53:17)
- 6 (2006-09-19 (火) 10:09:33)
- 7 (2007-01-11 (木) 12:35:28)
- 8 (2007-03-07 (水) 02:35:41)
- 9 (2007-04-11 (水) 17:48:48)
- 10 (2008-03-17 (月) 16:54:21)
- 11 (2008-11-23 (日) 21:42:05)
- 12 (2013-03-15 (金) 16:45:43)
- 13 (2013-09-01 (日) 01:42:51)
- 14 (2014-11-22 (土) 03:59:58)
- 15 (2014-11-25 (火) 03:03:31)
- 16 (2015-02-03 (火) 16:35:10)
- 17 (2016-09-08 (木) 20:33:38)
- 18 (2017-03-29 (水) 19:38:01)
- 19 (2018-03-02 (金) 14:17:42)
- 20 (2020-03-13 (金) 22:13:09)
- 21 (2021-09-15 (水) 10:27:34)
- 22 (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 --- * 概要 [#summary] `JTextArea`のカーソルがある行全体にアンダーラインを引きます。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTPL3eZj2I/AAAAAAAAAdk/KJTR3_NeAZE/s800/LineCursor.png) * サンプルコード [#sourcecode] #code(link){{ class LineCursorTextArea extends JTextArea { 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) { 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.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(); } // public static int getLineAtCaret(JTextComponent component) { // int caretPosition = component.getCaretPosition(); // Element root = component.getDocument().getDefaultRootElement(); // return root.getElementIndex(caretPosition) + 1; // } } }} * 解説 [#explanation] `JTextArea#paintComponent`メソッドをオーバーライドして、カーソルがある行にアンダーラインを引いています。 - `Caret`の移動に対応するため、`DefaultCaret#damage`メソッドをオーバーライドして、変更された領域を再描画 - `Caret`の移動に対応するため、`DefaultCaret#damage(Rectangle)`メソッドをオーバーライドして、変更された領域を再描画 - [http://www.jroller.com/page/santhosh/20050601?catname=%2FSwing Highlighting Current Line ]のように、`Highlighter`を使用する方法もある * 参考リンク [#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 ] - [[JTextAreaに行ハイライトカーソルを表示>Swing/LineHighlighter]] * コメント [#comment] #comment - `Caret`の高さを使用するように変更しました。 -- &user(aterai); &new{2008-03-17 (月) 16:54:50}; #comment