Search code examples
javaswingjtabletablecellrenderer

Java Swing : Jtable Highlight ONLY part of a word in a cell


Hi this question is rather specific. For class we had to make some kind of library program.

I made my list displayed in a JTable and ive implemented a search window that also uses a JTable...I Made a custom CellRenderer to Highlight when the term search was contained in the Code of the book or the Title.

My question is right now it bold the WHOLE word....is it possible to make it only bold part of that word ?

right now ill also have in that class a function that gives me the indexes of start and end of the search term in the value of the cell. (find it at the end of the class under getSearchIndex(Object, String) )

here is a screen and below the code for the renderer (the one for colors is separate).

Sorry this is in French

import java.awt.Component;
import java.awt.Font;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

 class HighlightRenderer extends DefaultTableCellRenderer {

private static final long serialVersionUID = 1L;
String searched = "";

public HighlightRenderer(String search){
    super();

    if(search != null && search != "")
        searched = search;
    else searched = "";
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
    Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if(searched.length() == 0){
            cellComponent.setFont(new Font(this.getFont().getName(), Font.PLAIN, this.getFont().getSize()));
        }

        else if(value.toString().toUpperCase().contains(searched.toUpperCase())){
            cellComponent.setFont(new Font(this.getFont().getFontName(), Font.BOLD, this.getFont().getSize()));
            int[] index = getSearchIndex(value, searched);
        }

    return cellComponent;
}

private int[] getSearchIndex(Object value, String search){

    int searchLength = search.length();
    String key = (String) value;

    int[] retour = new int[2];
    retour[0] = -1;
    retour[1] = -1;

    for(int i = 0; i < key.length(); i++){
        if(key.substring(i, i+searchLength).equalsIgnoreCase(search)){
            retour[0] = i;
            retour[1] = i + searchLength;
            return retour;
        }
    }
    return retour;

}

}

Thanks in advance for any tip or tricks.


Solution

  • Text components can have a StyledDocument and have parts of the text marked by specifying color attributes.

    A bit cheaper is to use HTML. Any text component, like the JLabel what the cell renderer offers by default. The HTML can be very partial.

    JLabel label = (JLabel) cellComponent; // Or new JLabel();
    label.setText(
        "<html>An <span style='background-color: lightskyblue'>example</span> of HTML");
    
    String highlight(String text, String sought) {
         text = StringEscapeUtils.escapeHTML4(text); // <, >
         sought = StringEscapeUtils.escapeHTML4(sought);
         return "<html>" + text.replace(sought, "<b>" + sought + "</b>");
    }