How can I highlight the current row in a JTextArea?

M

Markus

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
 
B

Bjorn Abelli

...
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
 
K

Knute Johnson

Markus said:
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

Take a look at JTextComponent.select() and
JTextComponent.setSelectedTextColor().
 
A

Alan Moore

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();
}
}
}
 
A

Alan Moore

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.
 
Joined
Jul 2, 2010
Messages
1
Reaction score
0
Are there any way to highlight String (line) before writing as rtf file?

I write code in Java so that program will extract some lines which meets some requirements in a log file and should write it back to a text file or rtf file, with those selected lines highlighted and all other lines as normal. Are there any way to highlight String (line) before writing as rtf file? I have coded other requirements, but unable to highlight the lines.
please help on this.
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top