Question

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?

Was it helpful?

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.

OTHER TIPS

You may also want to consider the Table Row Rendering Approach which may give you a little more flexibility in controlling which cells you change the font for. I've used it for bolding the text in all columns of the selected row.

Setting font to bold with caching, as described already here, will work.

In case you need to set only part of text in bold - use HTML. Table cell renderers are based on JLabel (or you can return one). Converting your text to html will allow almost any text attribute change.

We use this technique extensively and did not see any significant performance degradation.

Here's the lazy man's approach: Use DefaultTableCellRenderer (which is a subclass of JLabel) and use HTML to specify when you wish to use a bold typeface.

It won't be as performant as defining your own custom renderer and controlling the fonts directly, but the code is generally more compact so is good for simple applications.

/**
 * Renderer implementation for rendering Strings.
 * Strings beginning with 'A' are rendered in bold.
 */
public class MyRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {

    String txt = String.valueOf(value);

    if (txt != null && txt.startsWith("A")) {
      // Reassign value as an HTML string.
      // Obviously need to consider replacing HTML special characters
      // if doing this properly.
      value = String.format("<body><b>%s</b></body>", txt);
    }

    // Delegate to superclass which will set the label text, background, etc.
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  }
}

you can use this also..

        class SampleRenderer extends DefaultTableCellRenderer
        {

        public Component getJtableCellRendererComponent(Jtable table,Object value,boolean     isSelected , boolean hasFocus , int row, int column)

        {

        JLabel c = (JLabel)super.getJtableCellRendererComponent(table,value,isSelected ,hasFocus , row, column);

        Font f = c.getFont();

        c.setFont(f.getName(),Font.BOLD,f.getSize()));

        return c;

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top