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 Columns in JTable 
 

In this section, you will learn how to shading column in JTable.

 

Shading Columns in JTable

                         

Earlier you have read about the shading of  the rows in JTable. So, you are now capable for setting the shading the column in JTable.  In JTable component the shading columns are the simplest way of shading alternate columns in JTablecomponent that overrides the prepareRenderer() method. The following code helps you for setting the shading columns in JTable.

Description of program:

First of all the given code creates a table with specified column with headers by using JTableHeader that contains some data in it. The background color of the column header is yellow and the text color is black. After this, you need to shade the alternate columns (Odd place's row) in JTable component that overrides the prepareRenderer() method. The table calls the prepareRenderer method for every cell that is used to display and override method calls the super class and retrieves the prepareRenderer components. Then you have to get a colorful column of JTable.

Here is the code of program:

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

public class SaddingColumns{
  public static void main(String[] args) {
    new SaddingColumns();
  }
  public SaddingColumns(){
    JFrame frame = new JFrame("Sadding Columns in a JTable");
    JPanel panel = new JPanel();
    String data[][] {{"100","Vinod","programmer","5000"},
                      {
"101","Deepak","Content Writer","20000"},
                      {
"102","Noor","Techniqual Writer","30000"},
                      {
"104","Rinku","PHP programar","25000"}};
    String col[] {"Emp_Id","Emp_name","Emp_depart","Emp_sal"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    //Setting the sadding in column
    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);
        //odd col index, selected or not selected
        if(index_col % != && !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.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: c string com class orm ant form object io sed format screen font display vi metrics id render oo define

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 
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.