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 Cell Values in JTable

                         

In this section, we will learn how to set the cell values in JTable component. For this you must have the some previous knowledge about JTable. A cell is known as the format of  a row and a column in  JTable containing data in it. Each cell has it's own address where the data is stored. 

Description of program:

In this program, you will see the process of  setting the cell values in JTable. This program sets the data in the specified location in terms of cell address in the JTable. We have set the value in the 3rd rows and 3rd column in the below program.  This cell address has 'Math' value. Another cell address is 4 row and 1 column. The cell value of  that position is specified as  'Santosh'.  To set the cell values in the desired position in the JTable,  we created  the SetData method and set the cell value by applying  setValueAt() method on  the specified position in the JTable.

Description of code:

getModel():
This method returns the TableModel that displays the data of JTable.

setValueAt(Object obj, int row_index, int col_index):
This method is used to set the given value in a cell of  JTable at the specified row and column address. It takes the following parameters:

        obj: This is the new cell value that is used to add a data  in JTable.
        row_index: This is the index of row that is  to be changed.
        col_index: This is the index of column that is to be changed.

Here is the code of program:

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

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

  public SetCellValues(){
    JFrame frame = new JFrame("Setting Cell Values in JTable");
    JPanel panel = new JPanel();
    String data[][] {{"Vinod","MCA","Computer"},
                       {
"Deepak","PGDCA","History"},
                       {
"Ranjan","M.SC.","Biology"},
                       {
"Radha","BCA","Computer"}};
    String col[] {"Name","Course","Subject"};  
    DefaultTableModel model = new DefaultTableModel(data, col);
    table = new JTable(model);
    SetData("Math",2,2);
    SetData("Santosh",3,0);
    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.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void SetData(Object obj, int row_index, int col_index){
    table.getModel().setValueAt(obj,row_index,col_index);
    System.out.println("Value is added");
  }
}

Download this example.

Output of program:

Before setting cell values:

After setting cell values:

 

                         

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:

Do me favour....?, I'm Newbie

i've been frustated about JTable,i've problem to querying record onto JTable.The Case is:

1. i wanna querying records into JTable but i want to search the record when i type into cell and the record result into JTable.

2. i wanna records When the program running in the blank position

examples :
i wanna search The Name of James and all about james result into JTable

position :
=================================================
|Names | Nick Names |Address | Gender | Phone |
=================================================
..... | | | | |
=================================================
^
|
Type here and the records results into JTable, results :

=================================================
|Names | Nick Names |Address | Gender | Phone |
=================================================
James |Bond |Lake City| Male |12345 |
=================================================

can you help me,please give me the sample source code and send into my email address,cos i'm newbie and start to begins java program.Thank Before and Thanks a lot.

Posted by Fajar Arianto on Tuesday, 04.1.08 @ 22:17pm | #55053

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

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

Copyright © 2007. All rights reserved.