Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to highlight text WITHIN a JTable cell?

843806Dec 8 2008 — edited Jan 20 2009
I am writing a search/replace function that is intended to work with Swing JTables which contains Strings. I would like to be able to highlight a substring WITHIN a cell, if that substring is the sought-for value.
For example, if a single cell contained "Now is the time", I would like to be able to highlight only "the".

With the following code I can programmatically "select" characters 7-9 of the String:

table.editCellAt(row, col);
DefaultCellEditor ed = (DefaultCellEditor)table.getCellEditor();
JTextField jf = (JTextField)ed.getComponent();
jf.select(7, 10);

However, this does not visibly highlight the text; it merely sets the (invisible) caret to these character positions, so when the user begins to type characters 7-9 are replaced.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2009
Added on Dec 8 2008
24 comments
3,997 views