[nbusers] Re: alternate color for rows in jtable

  • From: Petr Podzimek < >
  • To:
  • Subject: [nbusers] Re: alternate color for rows in jtable
  • Date: Sat, 30 Jun 2012 07:40:00 +0200

code you have is from class that overrides JTable.prepareRenderer(...) method. Its nice trick.
Here you can see how to use TableCellRenderers:


Petr Podzimek.

On 06/29/2012 01:58 PM, sasabn wrote:
" type="cite">
I am beginner in Java and my English is not perfect. I am making desktop application using Netbeans which connect to mysql database. I picked up table from pallete and i populated it with data from coresponding table from MYSQL database. That is working fine. I made jtable using Defaultablemodel.
Now, I want to set background color of rows alternativly, odd rows with one color and even with another. I do not know how to do that. I found many examples on Internet but I don't know to implement it in my code. There is my code :


private String[] column = new String[]{
        "ID", "First Name", "Second Name", "wear of birth"};
    private Object[][] row = new Object[][]{};
    public DefaultTableModel modelTabele = new javax.swing.table.DefaultTableModel(redovi, kolona) {
        public boolean isCellEditable(int redovi, int kolona) {
            return false;

Now there is one of part of code which I found on Internet:

public Component prepareRenderer
  (TableCellRenderer renderer,int Index_row, int Index_col) {
  Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
  //even index, selected or not selected
  if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
  else {
  return comp;

I tried but I don't know to implement this part of code. 
Thanks in advance for helping.


[nbusers] alternate color for rows in jtable

sasabn 06/29/2012

[nbusers] Re: alternate color for rows in jtable

Petr Podzimek 06/30/2012

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 25 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20150226.965aeb8). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
Please Confirm