Swing/LineHighlighter のバックアップの現在との差分(No.12)
- category: swing folder: LineHighlighter title: JTextAreaに行ハイライトカーソルを表示 tags: [JTextArea, Caret, JViewport] author: aterai pubdate: 2006-07-24 pubdate: 2006-07-24T09:46:55+09:00 description: JTextAreaのカーソルがある行をハイライト表示します。 image:
概要
概要
JTextArea
のカーソルがある行をハイライト表示します。
Screenshot
Advertisement
サンプルコード
サンプルコード
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) {
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);
}
#spanadd
#spanend
@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);
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);
}
}
View in GitHub: Java, Kotlin解説
ほぼ、JTextAreaに行カーソルを表示とやっていることは同じです。解説
JTextAreaに行カーソルを表示と同様のコードを使用していますが、行全体を塗り潰すために以下の3
点を変更しています。
違うのは、以下の点になります。
-
Viewport
の色をscroll.getViewport().setBackground(Color.WHITE)
に変更 -
JTextArea#setOpaque(false)
で透明に設定 -
JTextArea#paintComponent(...)
のオーバーライドでカーソルのある行を塗りつぶしてから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 の Darryl.Burke さんのコード(以下に部分コピー)のように、
BasicTextPaneUI#paintBackground
をオーバーライドする方法(こちらの方がシンプルで美しいかも)もあります。
- Swing - Stretching background colour across whole JTextPane for one line of textの Darryl.Burke さんのコードのように
BasicTextPaneUI#paintBackground(...)
メソッドをオーバーライドする方法もある
#spandel
//JTextPane textPane = new JTextPane();
#spanend
#spandel
//textPane.setUI(new LineHighlightTextPaneUI(textPane));
#spanend
#spanadd
// https://community.oracle.com/thread/1364121
#spanend
#spanadd
// Swing - Stretching background colour across whole JTextPane for one line of text
#spanend
#spanadd
// JTextPane textPane = new JTextPane();
#spanend
#spanadd
// textPane.setUI(new LineHighlightTextPaneUI(textPane));
#spanend
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();
}
});
}
#spanadd
#spanend
@Override public void paintBackground(Graphics g) {
super.paintBackground(g);
try{
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) {
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
これらの方法なら、JTextEditor
やJTextPane
で行の高さが異なる場合でも、うまくハイライトできるようです。
- この場合、
JTextEditor
やJTextPane
で行の高さが異なる場合でもハイライト可能
参考リンク
参考リンク
- JTextAreaに行カーソルを表示
- Swing - Stretching background colour across whole JTextPane for one line of text
- Swing - Stretching background colour across whole JTextPane for one line of text