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

Setting the Column Header in JTable

                         

In the previous section, you have learnt about the creation of JTable without column headers. In this example you will learn how to set the column headers in JTable using JTableHeader. Java provides some method and APIs that helps you in setting the column headers in JTable. For more, go to the example.

Description of program:

This program will help you in setting the column headers in JTable. First of all, program creates a table that has some data in row and column format by using the JTable constructor. This table hasn't any column headers initially. For setting the column headers use the JTableHeader that helps you for constructing the JTableHeader that depends on its initialized column model in the TableColumnModel object. The header object adds in JScrollPane and JScrollPane adds in the Panel that will add in the frame which appears you on the screen. Exercise this and check it out. 

Description of code:

JTableHeader:
This is the constructor of JTableHeader class that is used for constructing the JTableHeader with initialized value in the JTableColumnModel. The object of JTableHeader is to manage the header of JTable.

getTableHeader():
This method takes table header from the JTable.

Here is the code of program:

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

public class SetColumnHeader{
  public static void main(String[] args) {
    new SetColumnHeader();
  }
  public SetColumnHeader(){
    JFrame frame = new JFrame("Setting the Column Header!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    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);
    JTable table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    panel.add(pane);
    frame.add(panel);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setSize(460,200);
    frame.setVisible(true);    
  }
}

 

 

 

 

 

 

 

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

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

i am not getting the table header.i have not used the defaulttablemodel class in my code.i just used JTaable tab=new JTable(data,col)

Posted by mano on Monday, 05.14.07 @ 17:09pm | #15858

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.

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

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

Copyright © 2008. All rights reserved.