Multiple rows colored differently in one JTable cell

Suppose you have one JTable, and for each cell you want to display three rows with a different color, for example, value 1 (red), value2 (blue), value3 (green).

I tried the getTableCellRendererComponentmethod DefaultTableCellRenderer, but setForeground(Color)it gives a unique color for the whole row shown in the cell.

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    String s = table.getModel().getValueAt(row,column).toString();

    StringTokenizer st = new StringTokenizer(s," ");
    int nToken=st.countTokens();

    value1=st.nextToken();
    value2=st.nextToken();
    value3=st.nextToken();
    // so now all the values are blue...
    setForeground(Color.blue);

    return super.getTableCellRendererComponent(table, value, isSelected,
            hasFocus, row, column);

}
+3
source share
2 answers

- JLabel. HTML-. , , - HTML . , , :

 <html><font color="blue">A String</font><font color="red">Another String</font></html>

, .

swing HTML, .

, , JComponent, paintComponent(), . . . "", . , , , .

HTML, .

+2

JIDE StyledLabel , , , 10x , - .

https://jide-oss.dev.java.net/

alt text

+3

Source: https://habr.com/ru/post/1708266/


All Articles