2

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?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Jason S
  • 184,598
  • 164
  • 608
  • 970

5 Answers5

5

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

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
5

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • hmmm... I don't get the difference, do you have any idea how this is different? – Jason S Feb 09 '10 at 13:17
  • This approach does not change "how" the bold font is derived. It only changes "where" you use the setFont(...) method to apply the bold font to the renderer. It is usefull when you have a table with multiple columns with different types of data. Instead of creating multiple renderers with custom code in each renderer to make the font bold, you only need to override a single method of JTable – camickr Feb 09 '10 at 15:58
1

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.

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
1

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);
  }
}
Adamski
  • 54,009
  • 15
  • 113
  • 152
0

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;

    }

}
user276002
  • 178
  • 3
  • 10
sreejith
  • 716
  • 5
  • 20