[nbusers] alternate color for rows in jtable

  • From: "sasabn" < >
  • To:
  • Subject: [nbusers] alternate color for rows in jtable
  • Date: Fri, 29 Jun 2012 07:58:24 -0400

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 :


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) {
   
        @Override
        public boolean isCellEditable(int redovi, int kolona) {
            return false;
        } 
}
    };



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


Code:
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)) {
  comp.setBackground(Color.lightGray);
  } 
  else {
  comp.setBackground(Color.white);
  }
  return comp;
  }



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

[/code]






[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 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close