Swing/LineHighlighter のバックアップの現在との差分(No.5)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTextAreaに行ハイライトカーソルを表示 #navi(../) *JTextAreaに行ハイライトカーソルを表示 [#b84f9cac] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-07-24~ 更新日:&lastmod; --- 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`のカーソルがある行をハイライト表示します。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTPQ5j7_JI/AAAAAAAAAds/kbet-1O8x-A/s800/LineHighlighter.png) **概要 [#x8bd671d] JTextAreaのカーソルがある行をハイライト表示します。 #screenshot **サンプルコード [#vabb7f2d] #code{{ * サンプルコード [#sourcecode] #code(link){{ class HighlightCursorTextArea extends JTextArea { private static final Color linecolor = new Color(250,250,220); 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) { @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); @Override protected void paintComponent(Graphics g) { Caret c = getCaret(); if (c instanceof DefaultCaret) { Graphics2D g2 = (Graphics2D) g.create(); DefaultCaret caret = (DefaultCaret) c; Rectangle r = SwingUtilities.calculateInnerArea(this, rect); r.y = caret.y; r.height = caret.height; g2.setPaint(LINE_COLOR); g2.fill(r); g2.dispose(); } super.paintComponent(g); } } }} -&jnlp; -&jar; -&zip; **解説 [#jb96e960] ほぼ、[[JTextAreaに行カーソルを表示>Swing/LineCursor]]とやっていることは同じです。 * 解説 [#explanation] [[JTextAreaに行カーソルを表示>Swing/LineCursor]]と同様のコードを使用していますが、行全体を塗り潰すために以下の`3`点を変更しています。 違うのは、以下の点になります。 -ViewPortの色をscroll.getViewport().setBackground(Color.WHITE)にする -JTextAreaをsetOpaque(false)と透明にする -JTextAreaのpaintComponent(Graphics g)をオーバーライドするとき、カーソルのある行を塗りつぶしてからsuper.paintComponent(g)する - `Viewport`の色を`scroll.getViewport().setBackground(Color.WHITE)`に変更 - `JTextArea#setOpaque(false)`で透明に設定 - `JTextArea#paintComponent(...)`のオーバーライドでカーソルのある行を塗りつぶしてから`super.paintComponent(g)`を実行 ---- [[Swing - Stretching background colour across whole JTextPane for one line of text>http://forum.java.sun.com/thread.jspa?threadID=5275222]] のように、BasicTextPaneUI#paintBackground をオーバーライドする方法(こちらの方がシンプルで美しいかも)もあります。 - [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)); // https://community.oracle.com/thread/1364121 // Swing - Stretching background colour across whole JTextPane for one line of text // 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() { 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(); 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(); } } } }} **参考リンク [#y021b082] -[[JTextAreaに行カーソルを表示>Swing/LineCursor]] -[[[[Swing - Stretching background colour across whole JTextPane for one line of text>http://forum.java.sun.com/thread.jspa?threadID=5275222]]]] - この場合、`JTextEditor`や`JTextPane`で行の高さが異なる場合でもハイライト可能 **コメント [#v72b7b28] - 行の折り返しに対応しました。 -- [[terai]] &new{2008-03-17 (月) 16:22:56}; #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