Highlighting Current Line

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. 
 * 
 * @author  Santhosh 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);
    

posted @ 2011-11-06 14:25  love25444  阅读(5)  评论(0)    收藏  举报