I want to highlight the current row of my JTextArea, such as in the
sourceeditor of Eclipse.
Does anybody know how I can do this?
Kind regards
Markus
Bjorn Abelli - 22 Nov 2005 14:13 GMT
"Markus" wrote...
> I want to highlight the current row of my JTextArea,
> such as in the sourceeditor of Eclipse.
>
> Does anybody know how I can do this?
Not with a JTextArea.
You should possibly use an JTextPane instead, or an own implementation of
JEditorPane.
Here's a stripped down version of an editor I wrote a couple of years ago...
//---------------------------------------
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
class JSyntaxArea
extends JEditorPane
{
StyledDocument document = null;
MutableAttributeSet defaultSyntax = new SimpleAttributeSet();
MutableAttributeSet highlightSyntax = new SimpleAttributeSet();
public JSyntaxArea ()
{
StyledEditorKit editorKit = new StyledEditorKit();
this.setEditorKit(editorKit);
document = (StyledDocument) this.getDocument();
// set colors for the different styles
StyleConstants.setBackground(highlightSyntax, Color.lightGray);
StyleConstants.setForeground(highlightSyntax, Color.RED);
StyleConstants.setBackground(defaultSyntax, Color.WHITE);
StyleConstants.setForeground(defaultSyntax, Color.BLACK);
}
private void setDefault()
{
int len = document.getLength();
String str = null;
document.setCharacterAttributes(0, len, defaultSyntax, false);
}
private void setHighlight (int offset, int len)
{
document.setCharacterAttributes(offset, len, highlightSyntax, false);
}
}
// Note, as it's a stripped down version, it may contain
// some bugs, even though I don't think so...
// Bjorn A
Knute Johnson - 22 Nov 2005 17:59 GMT
> I want to highlight the current row of my JTextArea, such as in the
> sourceeditor of Eclipse.
[quoted text clipped - 4 lines]
>
> Markus
Take a look at JTextComponent.select() and
JTextComponent.setSelectedTextColor().

Signature
Knute Johnson
email s/nospam/knute/
Alan Moore - 25 Nov 2005 01:47 GMT
You _can_ highlight the current line in a JTextArea, but select() is
not the way to do it. Here's a basic implementation of a current-line
highlighter:
import java.awt.Color;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.*;
/*
* Usage: textComp.addCaretListener(new CurrentLineHighlighter());
*/
public class CurrentLineHighlighter implements CaretListener
{
static final Color DEFAULT_COLOR = new Color(230, 230, 210);
private Highlighter.HighlightPainter painter;
private Object highlight;
public CurrentLineHighlighter()
{
this(null);
}
public CurrentLineHighlighter(Color highlightColor)
{
Color c = highlightColor != null ? highlightColor : DEFAULT_COLOR;
painter = new DefaultHighlighter.DefaultHighlightPainter(c);
}
public void caretUpdate(CaretEvent evt)
{
JTextComponent comp = (JTextComponent)evt.getSource();
if (comp != null && highlight != null)
{
comp.getHighlighter().removeHighlight(highlight);
highlight = null;
}
int pos = comp.getCaretPosition();
Element elem = Utilities.getParagraphElement(comp, pos);
int start = elem.getStartOffset();
int end = elem.getEndOffset();
try
{
highlight = comp.getHighlighter().addHighlight(start, end,
painter);
}
catch (BadLocationException ex)
{
ex.printStackTrace();
}
}
}
Alan Moore - 25 Nov 2005 06:01 GMT
I whipped up the code above just to show it could be done, but after
playing with it a bit, I discovered that the line highlighting tends to
get painted after selection highlighting. That means the selection (or
whatever portion of it is on the current line) isn't visible. The
easiest way to deal with that is to use a color with a very low alpha
component for line highlighting, like so:
static final Color DEFAULT_COLOR = new Color(0, 0, 0, 15);
I seem to remember hearing that alpha blending doesn't work on all
platforms (it works fine on my WinXP machine). In that case, you would
just have to ensure that the line highlighting get painted first. That
can be done too, but it takes a little bit of hackery.