Swing/LineCursor のバックアップの現在との差分(No.7)
TITLE:JTextAreaに行カーソルを表示
JTextAreaに行カーソルを表示
編集者:Terai Atsuhiro~
作成日:2006-01-30
更新日:2022-10-07 (金) 15:03:11
概要
JTextArea
のカーソルがある行全体にアンダーラインを引きます。
Screenshot
Advertisement
概要
JTextAreaのカーソルがある行全体にアンダーラインを引きます。サンプルコード
#spanend
#spanadd
class LineCursorTextArea extends JTextArea {
#spanend
private static final Color LINE_COLOR = Color.BLUE;
private DefaultCaret caret;
#spandel
#screenshot
#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();
}
-&jnlp;
-&jar;
-&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メソッドを変更して、描画に使われる領域を描画し直しています。
-
Caret
の移動に対応するためDefaultCaret#damage(Rectangle)
メソッドをオーバーライドして変更された領域を再描画 - MyBlog/CurrentLineHighlighter at master · santhosh-tekuri/MyBlog · GitHubのように
Highlighter
を使用する方法もある
参考リンク
- Swing - Line Number in JTextPane
- Swing - Line highlighting problem in presence of text highlighting!
- MyBlog/CurrentLineHighlighter at master · santhosh-tekuri/MyBlog · GitHub
- JTextAreaに行ハイライトカーソルを表示
参考リンク
- Java Forums - Line Number in JTextPane
- Java Technology Forums - Line highlighting problem in presence of text highlighting!
- Java低速GUI Swing 3
- Highlighting Current Line
コメント
-
Caret
の高さを使用するよう変更 -- aterai
コメント
- JEditorPaneではgetRowHeight()が使えないようなので、Java低速GUI Swing 3を参考にしてg2.getFontMetrics().getHeight()にしました。 -- terai