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

  • Home
  • SEARCH
  • 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 7958211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:23:09+00:00 2026-06-04T04:23:09+00:00

puuuuuuf, I’m starting to like swing :) I’m trying to write a cellRenderer to

  • 0

puuuuuuf, I’m starting to like swing 🙂 I’m trying to write a cellRenderer to customy render all cells besides those which in first row and column. So I wrote the following:

public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);

            if(row >0&&column>0){
                cell.setBackground(Color.GREEN);
            }

            return cell;
        }
    }

and set the renderer as following:

scheduleTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer()); 

but for some reason such an approach applies renderer to all the cell. So all of them are Green. If I’m doing something wrong, could you help me with that please?

Thanks in advance!

ADDITION

scheduleTable = new JTable() {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);
                        if (modelColumn != 0 && modelRow != 0) {
                            comp.setBackground(Color.GREEN);
                        }
                        return comp;
                    }
                };  

this code makes all the table green as well.

This code:

scheduleTable = new JTable(tableModel) {
                    @Override
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                        Component comp = super.prepareRenderer(renderer, row, column);
                        int modelRow = convertRowIndexToModel(row);
                        int modelColumn = convertColumnIndexToModel(column);

                            if (modelRow != 0 && modelColumn != 0) {
                                setBackground(Color.GREEN);
                            } else {
                                setBackground(Color.WHITE);
                            }

                        return comp;
                    }
                };

gives me the following result ;(
enter image description here

The following situations I have with differents n in expression row != 0 && column != 0:
enter image description here

  • 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-04T04:23:10+00:00Added an answer on June 4, 2026 at 4:23 am
    • you can use prepareRenderer, is easiest and more confortable that XxxCellRenderer

    • great code example is Table Row Rendering by @camickr

    EDIT:

    if (modelColumn != 0 || modelRow != 0) {
    

    enter image description here

    and with if (modelColumn != 0 && modelRow != 0) {

    enter image description here

    from code

    import java.awt.*;
    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) {
                    int modelRow = convertRowIndexToModel(row);
                    int modelColumn = convertColumnIndexToModel(column);
                    if (!isRowSelected(modelRow)) {
                        if (modelColumn != 0 || modelRow != 0) {
                            comp.setBackground(Color.GREEN);
                        } else {
                            comp.setBackground(table.getBackground());
                        }
                    }
                    return comp;
    
                    /*Component comp = super.prepareRenderer(renderer, row, column);
                    JComponent jc = (JComponent) comp;
                    Map attributes = (new Font("Serif", Font.PLAIN, 12)).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(new Font("Serif", Font.BOLD, 12));
                    }
                    } 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);
            getContentPane().add(scrollPane);
        }
    
        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

No related questions found

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.