Highlighting Current Line

本文介绍如何在JTextArea中实现当前行高亮的功能,通过自定义的CurrentLineHighlighter类,该类利用Swing组件的Highlighter功能,为任何JTextComponent添加当前行高亮特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Highlighting Current Line

Allmost all IDE's, have a feature of highlighting current line in editor. Today I tried to implement this for JTextArea(or any JTextComponent)

All Swing TextComponents supports Highlighter. If you have a habit of reading JDK code, your will found that the selected text is actually a highlighter. So why not add one more highlighter which highlights current line.

/** 
 * This class can be used to highlight the current line for any JTextComponent. 
 * 
 * @authorSanthosh Kumar T 
 * @version 1.0 
 */ 
public class CurrentLineHighlighter { 
private static final String LINE_HIGHLIGHT = "linehilight"; //NOI18N - used as clientproperty
private static Color col = new Color(255, 255, 204); //Color used for highlighting the line
 
// Installs CurrentLineHilighter for the given JTextComponent 
public static void install(JTextComponent c){ 
try { 
Object obj = c.getHighlighter().addHighlight(0, 0, painter); 
c.putClientProperty(LINE_HIGHLIGHT, obj); 
c.addCaretListener(caretListener); 
c.addMouseListener(mouseListener); 
}catch (BadLocationException ex) { 
} 
} 
 
// Uninstalls CurrentLineHighligher for the given JTextComponent 
public static void uninstall(JTextComponent c){ 
c.putClientProperty(LINE_HIGHLIGHT, null); 
c.removeCaretListener(caretListener); 
c.removeMouseListener(mouseListener); 
} 
 
private static CaretListener caretListener = new CaretListener(){ 
public void caretUpdate(CaretEvent e){ 
// todo: paint only interested region
((JTextComponent)e.getSource()).repaint(); 
} 
}; 
 
private static MouseAdapter mouseListener = new MouseAdapter(){ 
public void mousePressed(MouseEvent me){ 
Object obj = ((JTextComponent)me.getSource()).getClientProperty(LINE_HIGHLIGHT); 
((JTextComponent)me.getSource()).getHighlighter().removeHighlight(obj); 
// todo: paint only interested region
((JTextComponent)me.getSource()).repaint(); 
} 
public void mouseReleased(MouseEvent me){ 
try { 
JTextComponent c = ((JTextComponent)me.getSource()); 
Object obj = c.getHighlighter().addHighlight(0, 0, painter); 
c.putClientProperty(LINE_HIGHLIGHT, obj); 
}catch (BadLocationException ex) { 
} 
} 
}; 
 
private static Highlighter.HighlightPainter painter = new Highlighter.HighlightPainter(){ 
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c){ 
try { 
if(c.getSelectionStart()==c.getSelectionEnd()){ // if no selection 
Rectangle r = c.modelToView(c.getCaretPosition()); 
g.setColor(col); 
g.fillRect(0, r.y, c.getWidth(), r.height); 
} 
}catch (BadLocationException ignore) { 
} 
} 
}; 
}

how to use this:

CurrentLineHighlighter.install(textArea);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值