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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:18:09+00:00 2026-06-15T03:18:09+00:00

I have created one Jtable.This table consist of two columns name and timestamp. I

  • 0

I have created one Jtable.This table consist of two columns name and timestamp. I want to make color of row yellow if name is “jane”. Following is the code for that :-

    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);
        String name = table.getModel().getValueAt(row, 0).toString();

        if (name.trim().equals("jane")) {
            c.setBackground(Color.YELLOW);
        }
        return c;
    }
}

However instead of changing color of row to yellow for particular name, it is changing color of every row.I am setting data of table as following

tableModelName = (DefaultTableModel)jTableName.getModel();

jTableName.setDefaultRenderer(Object.class,new CustomRenderer());

for(int i=0; i<records.size(); i++)
         {
            tableModelName.addRow(records.get(i));          

         }

What I am doing wrong ?

  • 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-15T03:18:10+00:00Added an answer on June 15, 2026 at 3:18 am

    You need an else clause to set the background color to something besides yellow if the name is not “jane”. A single renderer instance is used for all rendering, so once you set the color to yellow on that instance, it stays yellow.

    Take a look at the JTable source code to see how the built-in renderers work:

        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        }
        else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
    

    For an easier way of doing this, you might try subclassing JTable and overriding prepareRenderer. This is handy for changes that affect entire rows like this, so you can use custom renderers for the individual cells, and tweak all renderers for a row in the prepareRenderer method.

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

Sidebar

Related Questions

I have created one jTable. I want to display the data into table from
I have created one table using the below command: create table Table1( Id int
I have created the following JTable and some buttons. One of the buttons (jbtClear)
I have created one dropdown box <select name=select id=selectId> <option id= value=calendar_Second_Calendar_1285942672.xml>Second Calendar</option> <option
I have created one application using XCode. Now i want to deploy that application
I have created one html form with following fields: Name (Text Box) Email (Text
I have created one application in which I want to start service when application
In Java Swing I have created a JTable which uses a table model class
I have created one application in which if i select on particular row in
I have created one stored procedure which runs on 5000 users in tbluser table

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.