Search code examples
javaswingjtabletablecellrenderercustom-renderer

CellRenderer - Making text bold


How to make the text in JTable bold, I tried the following, It does work for colours but for making the text bold i'm going wrong somewhere

    class ColourRender extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable tblData,
            Object value, boolean isSelected, boolean hasFocus, int row,
            int column) {
        Component cellComponent = super.getTableCellRendererComponent(
                tblData, value, isSelected, hasFocus, row, column);

        if (tblData.getValueAt(row, 3).equals("M")) {
            cellComponent.setForeground(forMen);

        } else {
            cellComponent.setForeground(forWomen);

        }
        return cellComponent;
    }

    class boldRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable tblData,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            Component cellComponent = super.getTableCellRendererComponent(
                    tblData, value, isSelected, hasFocus, row, column);

            if (tblData.getValueAt(row, 1).equals(bib)) {
                cellComponent.setFont(cellComponent.getFont().deriveFont(
                        Font.BOLD));
            } else {
                cellComponent.setFont(cellComponent.getFont().deriveFont(
                        Font.PLAIN));
            }

            return cellComponent;
        }
    }
}

and this in the class that with the table

tblData.getColumn("Bib no").setCellRenderer(new CustomRenderer());
tblData.getColumn("M/F").setCellRenderer(new CustomRenderer());

Solution

  • You create a bold font but do nothing with it: You never call setFont(...) anywhere.

    i.e.,

    if (tblData.getValueAt(row, 1).equals(bib)) {
       // you must call setFont(...) for a font to change
       cellComponent.setFont(cellComponent.getFont().deriveFont(Font.BOLD));
    } else {  
       // don't forget to set the font back if condition not true
       cellComponent.setFont(cellComponent.getFont().deriveFont(Font.PLAIN));
    }
    
    // changing foreground color:
    if (tblData.getValueAt(row, 3).equals("M")) {
       cellComponent.setForeGround(MEN_COLOR); // a constant
    } else {  
       cellComponent.setForeGround(myTable.getForeground());
    }
    

    For colors, set the foreground color to null in your else block to have your colors change back to default.


    Edit
    For example:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    public class CustomRendererExample extends JPanel {
       public static final String[] COLUMNS = { "M/F", "Fubar" };
       public static final String[] FUBARS = { "Fubar", "Snafu" };
       public static final String[] M_F = { "M", "F" };
       protected static final Color M_COLOR = Color.red;
       private DefaultTableModel dm = new DefaultTableModel(COLUMNS, 5);
       private JTable table = new JTable(dm);
    
       public CustomRendererExample() {
          add(new JScrollPane(table));
          DefaultCellEditor mfEditor = new DefaultCellEditor(new JComboBox<>(M_F));
          table.getColumn(COLUMNS[0]).setCellEditor(mfEditor);
          table.getColumn(COLUMNS[0]).setCellRenderer(
                new DefaultTableCellRenderer() {
                   @Override
                   public Component getTableCellRendererComponent(JTable table,
                         Object value, boolean isSelected, boolean hasFocus,
                         int row, int column) {
                      Component superRenderer = super.getTableCellRendererComponent(table, 
                            value, isSelected, hasFocus, row, column);
    
                      if (value != null && value.equals(M_F[0])) {
                         superRenderer.setForeground(M_COLOR);
                      } else {
                         superRenderer.setForeground(null);
                      }
    
                      return superRenderer;
                   }
                });
    
          DefaultCellEditor fubarEditor = new DefaultCellEditor(new JComboBox<>(
                FUBARS));
          table.getColumn(COLUMNS[1]).setCellEditor(fubarEditor);
          table.getColumn(COLUMNS[1]).setCellRenderer(
                new DefaultTableCellRenderer() {
                   @Override
                   public Component getTableCellRendererComponent(JTable table,
                         Object value, boolean isSelected, boolean hasFocus,
                         int row, int column) {
                      Component superRenderer = super.getTableCellRendererComponent(table, 
                            value, isSelected, hasFocus, row, column);
    
                      if (value != null && value.equals(FUBARS[0])) {
                         superRenderer.setFont(superRenderer.getFont().deriveFont(Font.BOLD));
                      } else {
                         superRenderer.setFont(superRenderer.getFont().deriveFont(Font.PLAIN));
                      }
    
                      return superRenderer;
                   }
                });
    
       }
    
       private static void createAndShowGui() {
          CustomRendererExample mainPanel = new CustomRendererExample();
    
          JFrame frame = new JFrame("Custom Renderer Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }