Home Java Example Java Swing Shading Rows in JTable
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Shading Rows in JTable

Advertisement
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:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Shading Rows in JTable   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
suhail
May 29, 2011
good

your posts are very helpful :)
Cody
June 11, 2013
Great Article!

Really helped me get to the point of knowing how to shade every other cell. Thank you!
Martin Jerenje
October 7, 2013
COOL.. NICE ONE

Thanks .. Benefit from it.. All the best
DMCA.com