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

Create a Custom Cell Renderer in a JTable

                         

After getting the knowledge about the JTable components and creation with column header, you will be able to create a custom cell renderer in a JTable component. Here, first of all you will know about the cell renderer in JTable. The cell renderer is the component of  JTable that can be used to display the cells in a column. The JTable describes a specific renderer to the particular column. For this the JTable invokes the table model to use the getColumnClass method that takes the data types of the column's cells.

The custom cell renderer means user created renderer according to  requirement. The given code with description is to provide the facility for creating a custom cell renderer in a JTable to use some Java methods and APIs.

Description of program:

In this program, you will see how to create a custom cell renderer. To create a table cell renderer you need a JTable, so that  this program can create a table containing the data and column with column header in the very first step. After that you will create a custom cell renderer to apply the CustomTableCellRenderer() method by using the setCellRenderer method that specify the cells in a particular column to use renderer. The CustomTableCellRenderer method extends the DefaultTableCellRenderer that helps you to create a default table cell renderer. Finally, it will create a custom cell renderer that will display in given output of table. The even rows of a JTable contains cyan color and odd rows contains lightGray color but when you select it, the selected row or rows will look like green color. 

Description of code:

DefaultTableCellRenderer():
This is the constructor of DefaultTableCellRenderer class that can be used for displaying the individual cells in a JTable. It constructs a default table cell renderer.

getTableCellRendererComponent( JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column):
This is the method that returns default table cell renderer. It takes the following agruments:

        table: This is a JTable.
        obj: This is the value that is assigning in the cells at the specified positions (rows and columns).
        isSelected: When any cell is selected then it becomesl true.
        hasFocus: This the the specific cell that is true when you focus any cell of a JTable.
        row: This is the row of cell renderer.
        column: This is the column of cell renderer.

isSelected():
This method returns the state of column either true or false. If any cell is selected then it will return true otherwise false.

Here is the code of program:

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

public class CustomCellRenderer{
  JTable table;
  TableColumn tcol;
  public static void main(String[] args) {
    new CustomCellRenderer();
  }

  public CustomCellRenderer(){
    JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
    JPanel panel = new JPanel();
    String data[][] {{"Vinod","Computer","3"},
                       {
"Rahul","History","2"},
                       {
"Manoj","Biology","4"},
                       {
"Sanjay","PSD","5"}};
    String col [] {"Name","Course","Year"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    table = new JTable(model);
    tcol = table.getColumnModel().getColumn(0);
    tcol.setCellRenderer(new CustomTableCellRenderer());
    tcol = table.getColumnModel().getColumn(1);
    tcol.setCellRenderer(new CustomTableCellRenderer());
    tcol = table.getColumnModel().getColumn(2);
    tcol.setCellRenderer(new CustomTableCellRenderer());
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    panel.add(pane);
    frame.add(panel);
    frame.setSize(500,150);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public class CustomTableCellRenderer extends DefaultTableCellRenderer{
    public Component getTableCellRendererComponent (JTable table, 
Object obj, 
boolean isSelected, boolean hasFocus, int row, int column) {
      Component cell = super.getTableCellRendererComponent(
                         table, obj, isSelected, hasFocus, row, column
);
      if (isSelected) {
        cell.setBackground(Color.green);
      
      else {
        if (row % == 0) {
          cell.setBackground(Color.cyan);
        }
        else {
          cell.setBackground(Color.lightGray);
        }
      }
      return cell;
    }
  }
}

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

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

Through setSize() ,we are setting the size of the frame only and the Table remains as it is. Plz tell me the way by which i can adjust the size of Table also.
Thank you,

Posted by Ashish on Tuesday, 01.15.08 @ 16:29pm | #45257

this tut very helpful to me. and I want to align the jTable cell value in right. because when currency type value should apper with right alignment. so please If any one know it tell me.

Posted by indu on Monday, 04.23.07 @ 09:12am | #14786

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

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

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

Copyright © 2007. All rights reserved.