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


 
  
 
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
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

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 
Latest Searches:
display arraylist
jstl basics tomcat
how to read and wirte
add the ArrayList to
JSP Cookies Example
Introduction to Spring
Insert Data into Datab
source code to send te
ant pdf
ajax tree example
show calender page
richfaces a4j taglibr
jQuery To Hide the Div
gridsphere projects
for loop
Read/Write XML with DO
struts.jar download
delete
t:htmltag
<t:dataScroller
servermail service pro
login eample code usin
java Concurrency utili
file uploading code us
javaBeans class exampl
Creating a DSN for MyS
abstract class and abs
session login in jsp
velocity rules engine
CHECKLIST BOX
action form struts
HWPF
login session
Vector
how to run applets
tree
to find the largest el
how to generate pdf fi
Framework for Java Dat
Use JFrame in JSP code
draw Polyline
standered jstl jar to
how to retrieve data f
iport syntax in java
java random numbers
Sending mails using ja
Define JSP
POST METHOD
Display name of system
communication
PHP Database Related M
Java Pass Value
Get Array Size
SIMPLE JAVA SOURSE COD
SaveErrors
getter setter methods
The JDBC(tm) Universal
text area
Jigloo
decimal to binary conv
Scrum
insert data to text fi
standard deviation arr
connect to sql databas
insert content to text
calender class
swap arrays in java
Photoshop Text Effects
java excel
decimal to unicode
string Compareing
how to get system scre
java write file line b
Area and circumference
\n java
jboss connection pool
how to print arraylist
Display name of system
login code example in
netbeans jsp
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 | 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.