Swing/LineHighlighter のバックアップソース(No.16)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/LineHighlighter へ行く。
- 1 (2007-04-10 (火) 16:27:02)
- 2 (2007-08-02 (木) 11:14:23)
- 3 (2008-03-17 (月) 16:22:35)
- 4 (2008-03-17 (月) 17:23:03)
- 5 (2008-03-17 (月) 18:51:09)
- 6 (2012-10-19 (金) 20:14:31)
- 7 (2013-02-27 (水) 13:49:26)
- 8 (2013-03-15 (金) 16:56:20)
- 9 (2013-08-30 (金) 01:37:31)
- 10 (2013-10-23 (水) 20:27:47)
- 11 (2014-11-22 (土) 03:59:58)
- 12 (2014-11-25 (火) 03:03:31)
- 13 (2014-12-30 (火) 15:42:38)
- 14 (2016-04-13 (水) 17:45:02)
- 15 (2016-09-18 (日) 21:06:37)
- 16 (2017-11-01 (水) 15:18:58)
- 17 (2019-05-22 (水) 17:25:00)
- 18 (2021-02-14 (日) 22:46:58)
- 19 (2024-01-24 (水) 23:47:38)
--- category: swing folder: LineHighlighter title: JTextAreaに行ハイライトカーソルを表示 tags: [JTextArea, Caret, JViewport] author: aterai pubdate: 2006-07-24T09:46:55+09:00 description: JTextAreaのカーソルがある行をハイライト表示します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTPQ5j7_JI/AAAAAAAAAds/kbet-1O8x-A/s800/LineHighlighter.png --- * 概要 [#summary] `JTextArea`のカーソルがある行をハイライト表示します。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTPQ5j7_JI/AAAAAAAAAds/kbet-1O8x-A/s800/LineHighlighter.png) * サンプルコード [#sourcecode] #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); } } }} * 解説 [#explanation] ほぼ、[[JTextAreaに行カーソルを表示>Swing/LineCursor]]と同様のコードを使用していますが、行全体を塗り潰すために、以下の`3`点を変更しています。 - `Viewport`の色を`scroll.getViewport().setBackground(Color.WHITE)`にする - `JTextArea#setOpaque(false)`と設定して透明にする - `JTextArea#paintComponent(...)`をオーバーライドするとき、カーソルのある行を塗りつぶしてから`super.paintComponent(g)`する ---- [https://community.oracle.com/thread/1364121 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() { @Override 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`で行の高さが異なる場合でも、うまくハイライトできるようです。 #ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTPTaywxYI/AAAAAAAAAdw/RIlfRHiC-JY/s800/LineHighlighter1.png) * 参考リンク [#reference] - [[JTextAreaに行カーソルを表示>Swing/LineCursor]] - [https://community.oracle.com/thread/1364121 Swing - Stretching background colour across whole JTextPane for one line of text] * コメント [#comment] #comment - 行の折り返しに対応しました。 -- &user(aterai); &new{2008-03-17 (月) 16:22:56}; #comment