Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:40:48+00:00 2026-06-06T14:40:48+00:00

Edit: [Solved] : It seems that the getTableCellRendererComponent of the CustomTableCellRenderer is called every

  • 0

Edit: [Solved]: It seems that the getTableCellRendererComponent of the CustomTableCellRenderer is called every time I’m trying to increase and decrease the font size.

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 (this is because this value will affect the font size of all tables that are currently being shown in my UI)

I’ve updated the code to take care of this. Any comments/feedback on this would be welcome. Thanks once again!


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

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.

The code for creating the table is as follows:

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());

The custom TableCellRenderer is implemented as follows:

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;
    }

}

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:

if(table.getDefaultRenderer(table.getColumnClass(0)) instanceof CustomTableCellRenderer){
    CustomTableCellRenderer tableCellRenderer = (CustomTableCellRenderer) table.getDefaultRenderer(table.getColumnClass(0));
    tableCellRenderer.increaseFontSize();
    table.repaint();            
}

However I see no change in the table. What am I doing wrong? And how do I achieve the desired functionality?

Any comments/suggestions would be really appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:40:50+00:00Added an answer on June 6, 2026 at 2:40 pm
    • basically you have to set JTable#setFont()

    • in the Renderer you can increase FontSize depends of

      a) value in the TableCell

      b) row or column index

      c) isSelected and hasFocus

    • in the Renderer is better to override for isSelected and hasFocus

    • for JTable without changes in the TabelModel and ColumnModel to use prepareRender exclusivelly (my view I love prepareRenderer)

    EDIT

    I’d be to change JTable#setFont(), rather than increase that into Renderer, then to apply changed Font to the Row, Column or Selection

    enter image description here

    enter image description here

    enter image description here

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.font.TextAttribute;
    import java.util.Map;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.MatteBorder;
    import javax.swing.table.*;
    
    public class TablePrepareRenderer extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JTable table;
    
        public TablePrepareRenderer() {
            Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
            Object[][] data = {
                {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
                {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
                {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
                {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
            };
            DefaultTableModel model = new DefaultTableModel(data, columnNames) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
            table = new JTable(model) /*{
    
                    private static final long serialVersionUID = 1L;
                    private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
                    private Border inside = new EmptyBorder(0, 1, 0, 1);
                    private Border highlight = new CompoundBorder(outside, inside);
    
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                    Component comp = super.prepareRenderer(renderer, row, column);
                    JComponent jc = (JComponent) comp;
                    Map attributes = (table.getFont()).getAttributes();
                    //attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
                    attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                    if (!isRowSelected(row)) {
                    comp.setForeground(Color.black);
                    comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);
                    int modelRow = convertRowIndexToModel(row);
                    String type = (String) getModel().getValueAt(modelRow, 0);
                    if (type.equals("Sell")) {
                    comp.setFont(new Font(attributes));
                    comp.setForeground(Color.red);
                    } else {
                    comp.setFont(table.getFont());
                    }
                    } else {
                    comp.setFont(table.getFont());
                    }
                    jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));
                    return comp;
                    }
                    }*/;
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
            JButton increase = new JButton("+");
            increase.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Font font = table.getFont();
                    font = font.deriveFont((float) (font.getSize2D() * 1.10));
                    table.setFont(font);
                }
            });
            add(increase, BorderLayout.EAST);
            JButton decrease = new JButton("-");
            decrease.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Font font = table.getFont();
                    font = font.deriveFont((float) (font.getSize2D() * .90));
                    table.setFont(font);
                }
            });
            add(decrease, BorderLayout.WEST);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TablePrepareRenderer frame = new TablePrepareRenderer();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: Solved my own problem, it seems. See the edits. I am trying to
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
edit: Solved - mod_rewrite was the problem I can't get CI to work as
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
EDIT: solved it, turns out I should use %c not %s because foodSelect and
[EDIT: Problem solved. Please see my answer below.] In my app I call the
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
Edit 4/4/12 I STILL HAVE ONE QUESTION: I solved my issue but it adds
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.