Search code examples
javaswingfontsjtablecellrenderer

Swing: TableCellRenderer that uses boldface for some of the cells


Simple question, but I can't seem to find the answer anywhere online.

How do you use a custom TableCellRenderer to render some of the table cells in boldface?

I know how to use TableCellRenderer to set the background color on a cell-by-cell basis. You do something like:

  public class MyTableCellRenderer extends DefaultTableCellRenderer 
  {
    @Override public Component getTableCellRendererComponent(JTable table,
       Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value,
          isSelected, hasFocus, row, column);
        // modify the component "c" to have whatever attributes you like
        // for this particular cell
    }
  }

I would assume changing the rendering text style is similar, but how do you set the font to be the same as the default table font but in boldface?


Solution

  • If you can already get the default table font (which I suppose would be c.getFont()), then just use deriveFont(Font.BOLD) on it.