Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Shading Rows in JTable 
 

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.

 

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:

                         

» View all related tutorials
Related Tags: c com class orm ant form model io components sed color format component value byte int rgb show pixel for

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.

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

Current Comments

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

Thanks,
This has helped me in the program I was busy with tremendously, however, the setSelectionMode(0) (This is to set selection by row) now no longer functions with this enabled. Confusing I will continue trying to figure this out.

Posted by Craig on Wednesday, 12.10.08 @ 00:59am | #82584

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.