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 206531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:39:16+00:00 2026-05-11T17:39:16+00:00

This is driving me absolutely insane. I know that, to change the formatting of

  • 0

This is driving me absolutely insane.

I know that, to change the formatting of table cells with JTable, I have to use my own renderer. But I cannot seem to implement this properly.

This is my current setup:

public class MyClass
{
    public static void main(String args[])
    {
        JTable myTable = new JTable(10, 10);
        myTable.setDefaultRenderer ([I dont know what to put here], new CustomRenderer());
    }
}

class CustomRenderer 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);

        // Formatting
        return c;
    }
}

What do I need to use for the first parameter of setDefaultRenderer? The API just says ‘class’. I have no idea what to put there.

Could someone just explain, in the simplest of terms, how I go about implementing this? Please provide an example of how I can change the formatting from within the main() method as well.

  • 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-05-11T17:39:16+00:00Added an answer on May 11, 2026 at 5:39 pm

    In the first parameter for setDefaultRenderer, put the class literal for the Class that you want to override rendering. I.e., if your data consist all of strings, you can put

    myTable.setDefaultRenderer(String.class, new CustomRenderer());
    

    If your data also consists of values with BigDecimal or Integer as classes, you have to invoke that method several times for each class type (BigDecimal.class or Integer.class in each case).

    And finally, to change the background color you do this in your renderer:

    class CustomRenderer 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);
            c.setBackground(new java.awt.Color(255, 72, 72));
            return c;
        }
    }
    

    If you write a renderer that should work for all classes of an interface, you will also need to modify the getColumnClass function of your table model and let it return the interface class for all objects that implement this interface:

    public Class<? extends Object> getColumnClass(int c) {
        Object object = getValueAt(0, c);
        if(object == null) {
            return Object.class;
        if(getValueAt(0, c) instanceof IColorable) {
            return ICarPart.class;
        } else {
            return getValueAt(0, c).getClass();
        }
    }
    

    With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is driving me absolutely insane, especially because I have a suspicion that
This is driving me absolutely crazy. I have a package that was working fine,
This is driving me crazy. I have a PHP script that gets some data
I have been having a wierd issue that is driving me absolutely crazy. It
This has been driving me absolutely nuts. I have a substitute function like this:
This is driving me completely insane. I have Disqus installed on my site and
This is absolutely driving me crazy. I have the Droid X 2.3.3. For some
This problem is driving me a bit insane - I have spent the best
I have a very simple query that is driving me absolutely nuts. Here's the
This is driving me insane. I have a node app with express. It serves

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.