views:

250

answers:

5

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?

+5  A: 

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
is there a significant cost in doing so every time a cell is rendered? or should I cache the bold font?
Jason S
If the table's font never changes, then cache it by all means. I don't know how expensive the call is.
Michael Myers
+5  A: 

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
hmmm... I don't get the difference, do you have any idea how this is different?
Jason S
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
+1  A: 

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.

eugener
+1  A: 

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
Using HTML parsing capacities of the JLabel helped me a lot, many times
Laurent K
A: 

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;

}

}

sreejith