Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Shading Rows in JTable

                         

You have learnt about the JTable components and its column headers in the previous Java section. Now, you will be able to do the shading rows in JTable. But, the question arises what is shading?. 

Shading Rows: In JTable component the shading rows are the simplest way of shading alternate rows in JTablecomponent that overrides the prepareRenderer() method.

This section provides you a simple way for shading the alternate rows in JTable by using the some Java methods and APIs this alternate rows appear in the color. 

Description of program:

This program creates a table with some data and column with headers by using the setBackground() method that displays a yellow background in the column header. After that you need to shade the alternate rows (even rows) in JTable component that overrides the prepareRenderer() method. This table calls the prepareRenderer method to every cell that is used to display and override method calls the super class and retrieves the prepareRenderer components. It will modify its background color and achieve the shaded rows of a table.

Description of code:

prepareRenderer(TableCellRenderer renderer, int index_row, int index_col):
This method returns the component under the specified event location. It prepares the table cell renderer by querying the data model to value and selection state of the cell at rows or columns. It takes the following arguments:

        renderer: The TableCellRenderer that have to prepare.
        index_row: This is the index of row of the cell renderer, where '0' denotes the first row.
        index_col:  This is the index of column of the cell renderer, where '0' represents the first column.

TableCellRenderer: This is an interface that defines the method for requiring object that have to renderer for cell in a JTable.

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class SadingRows{
  public static void main(String[] args) {
    new SadingRows();
  }
  public SadingRows(){
    JFrame frame = new JFrame("Shading Rows in a JTable");
    JPanel panel = new JPanel();
    String data[][] {{"Vinod","100","MCA","Computer"},
                      {
"Deepak","101","PGDCA","History"},
                      {
"Ranjan","102","M.SC.","Biology"},
                      {
"Radha","103","BCA","Computer"}};
    String col[] {"Name","Roll","Course","Subject"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    //Setting the sadding in rows
    JTable table = new JTable(model){
      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 % == && !isCellSelected(Index_row, Index_col)) {
          comp.setBackground(Color.lightGray);
        
        else {
          comp.setBackground(Color.white);
        }
        return comp;
      }
    };
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
      JScrollPane pane = new JScrollPane(table);
    panel.add(pane);
    frame.add(panel);
    frame.setSize(460,200);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Download this example.

output of program:

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.