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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:33:09+00:00 2026-06-06T04:33:09+00:00

I want to color specific rows in jTable..i did it for columns by using

  • 0

I want to color specific rows in jTable..i did it for columns by using this code,

private class CustomCellRenderer extends DefaultTableCellRenderer {

/* (non-Javadoc)
 * @see    
javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
 */

    @Override
public Component  getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {

  Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);

//Set foreground color
// rendererComp.setForeground(Color.red);
//Set background color
  rendererComp .setBackground(Color.pink);

 return rendererComp ;
 }

}

And i call the above code using,

 jTable1.getColumnModel().getColumn(3).setCellRenderer(new CustomCellRenderer());

But i want to do the same for rows in jTable..There’s no getColumnModel() or getColumn() in the case of rows..So what’s the alternate way for doing that? I am doing it in Netbeans by using Java Swing..

  • 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-06T04:33:10+00:00Added an answer on June 6, 2026 at 4:33 am

    Here is an example on how you can combine both column colors and row color. You basically perform tests in the TableCellRenderer to see if the background should be of one color or another.

    import java.awt.Color;
    import java.awt.Component;
    import java.util.Enumeration;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    public class TestTable {
    
        public class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
    
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                setBackground(null);
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setText(String.valueOf(value));
                boolean interestingRow = row % 5 == 2;
                boolean secondColumn = column == 1;
                if (interestingRow && secondColumn) {
                    setBackground(Color.ORANGE);
                } else if (interestingRow) {
                    setBackground(Color.YELLOW);
                } else if (secondColumn) {
                    setBackground(Color.RED);
                }
                return this;
            }
    
        }
    
        private JFrame f;
        private JTable table;
    
        protected void initUI() {
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            Vector<String> columNames = new Vector<String>();
            columNames.add("Col 0");
            columNames.add("Col 1");
            columNames.add("Col 2");
            for (int i = 0; i < 20; i++) {
                Vector<Object> v = new Vector<Object>();
                v.add(i % 3 == 0 ? "Hello" : "World");
                v.add("Some data in row " + (i + 1));
                v.add("Some other data in row " + (i + 1));
                data.add(v);
            }
            table = new JTable(new DefaultTableModel(data, columNames));
            Enumeration<TableColumn> en = table.getColumnModel().getColumns();
            while (en.hasMoreElements()) {
                TableColumn tc = en.nextElement();
                tc.setCellRenderer(new MyTableCellRenderer());
            }
            f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.add(new JScrollPane(table));
            f.pack();
            f.setVisible(true);
    
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestTable().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the color of the Pixel at a specific Touch Point
I have a grid containing rows flagged with different priorities. I want to color
I want to set color for specific lines in the text area. What I've
I want to implement a custom collection that contains instances of my class. This
I have one image with multiple colors. I want to change a specific color
I want the header cells of a table to have a specific border color
I want to set the font color of a cell to a specific RGB
If I set a CSS value on a specific element using: $('#element').css('background-color', '#ccc'); I
I want to change the color of one specific cell in my table: Why
I want to be able to change this color: <td bgcolor="**LightGrey**">row 2, cell 1</td>

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.