How to highlight text WITHIN a JTable cell?
843806Dec 8 2008 — edited Jan 20 2009I 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.