Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

How can I highlight the current row in a JTextArea?

Thread view: 
Markus - 22 Nov 2005 11:38 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.