title: Dynamically Changing JTable Font Size
body: <hr>
  
  <p><strong>Edit: [Solved]</strong>: It seems that the getTableCellRendererComponent of the CustomTableCellRenderer is called every time I'm trying to increase and decrease the font size. </p>
  
  <p>So I updated my code to take care of font size in this method itself. I'm using a static variable to keep track of the current font size (<em>this is because this value will affect the font size of all tables that are currently being shown in my UI</em>) </p>
  
  <p>I've updated the code to take care of this. Any comments/feedback on this would be welcome. Thanks once again!</p>
  
  <hr>
  
  <p>The issue that I'm trying to solve is to dynamically change (i.e. increase/decrease) the font size in a JTable based on a click on a different JLabel</p>
  
  <p>I have a panel that hosts two labels (for increasing/decreasing font size) and a JTable. To this table I've attached a custom TableCellRenderer. </p>
  
  <p>The code for creating the table is as follows:</p>
  
  <pre><code>JTable table = new JTable(data, tableHeader){
      @Override
      public Component prepareRenderer(TableCellRenderer renderer, int row,
              int column) {
          Component component = super.prepareRenderer(renderer, row, column);
          int rendererWidth = component.getPreferredSize().width;
          TableColumn tableColumn = getColumnModel().getColumn(column);
          tableColumn.setPreferredWidth(Math.max(rendererWidth +
                  getIntercellSpacing().width + 10,
                  tableColumn.getPreferredWidth()));
          return  component;
      }
  
  };
  table.setPreferredScrollableViewportSize(new Dimension(5000, 200));
  table.setFillsViewportHeight(true);
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  table.setDefaultRenderer(table.getColumnClass(0), new CustomTableCellRenderer());
  </code></pre>
  
  <p>The custom TableCellRenderer is implemented as follows:</p>
  
  <pre><code>public class CustomTableCellRenderer extends DefaultTableCellRenderer {
  
      private Font customFont;
  
      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);
  
          customFont = component.getFont();
          customFont = new Font(customFont.getName(), customFont.getStyle(), MainUI.DEFAULT_TABLE_FONT_SIZE);
          component.setFont(customFont);
  
          if (value.toString().matches("(?i:.*error.*)")) {
              c.setForeground(Color.RED);
          } else {
              c.setForeground(Color.BLACK);
          }
          return c;
      }
  
      public void increaseFontSize(){
          MainUI.DEFAULT_TABLE_FONT_SIZE = getFont().getSize() + 1;
      }
  
      public void decreaseFontSize(){
          MainUI.DEFAULT_TABLE_FONT_SIZE = getFont().getSize() + 1;
      }
  
  }
  </code></pre>
  
  <p>For the button's I've attached a mouse listener and on mouseClicked event, I'm trying to do the following to achieve the functionality:</p>
  
  <pre><code>if(table.getDefaultRenderer(table.getColumnClass(0)) instanceof CustomTableCellRenderer){
      CustomTableCellRenderer tableCellRenderer = (CustomTableCellRenderer) table.getDefaultRenderer(table.getColumnClass(0));
      tableCellRenderer.increaseFontSize();
      table.repaint();            
  }
  </code></pre>
  
  <p>However I see no change in the table. What am I doing wrong? And how do I achieve the desired functionality? </p>
  
  <p>Any comments/suggestions would be really appreciated. Thanks!</p>