TableCellRenderer - Set font color

dshrenik
dshrenik used Ask the Experts™
on
Please let me know how I can set the font color. Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011
Commented:
setForeground(Color) in CellRenderer, see below:
http://www.jguru.com/faq/view.jsp?EID=53764

Basically you need to install a custom cell renderer. You can easily create a custom cell renderer as follows. (You define SPECIAL_ROW and SPECIAL_COLUMN)


import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;

public class YourTableCellRenderer
       extends DefaultTableCellRenderer {
  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);

    // Only for specific cell
    if (row == SPECIAL_ROW && column == SPECIAL_COULMN) {
       c.setFont(/* special font*/);
       // you may want to address isSelected here too
       c.setForeground(/*special foreground color*/);
       c.setBackground(/*special background color*/);
    }
    return c;
  }
}

To install the cell renderer use various methods from JTable


table.setDefaultRenderer(new YourTableCellRenderer());

or TableColumn class e.g.


TableColumn.setCellRenderer(new YourTableCellRenderer());

Open in new window

Awarded 2011
Awarded 2011

Commented:
It is basically the same as changing sell color - but instead of setBackground you make setForeground

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial