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

Inserting Rows in a JTable

                         

After making a table, you  need to insert the data in a table. This tutorial helps you in how to insert rows in a JTable to specified locations or places according to its field. For inserting a row in JTable component, you will require to insert it into its table model object.

Description of program:

This program creates a table by using the JTable constructor that contains 3 rows and 2 columns. If  you want to insert a data at any location by using the insertRow() method that contains its position and data, you have to define the position and data of  the inserting data. All the data will be  add in this table model. This table model supports for inserting the data in the row by using the DefaultTableModel.

Description of code:

DefaultTableModel(Object data[][], Object col[]):
This method creates a DefaultTableModel and initializes the table that will pass in it. It takes the following arguments:

        data: This is the object that adds in a table.
        col: This is a column object that adds in the table.

insertRow(int row_index, Object data[]):
Above method is used to insert sa row at specified location. It takes the following parameters:

        row_index: This is the index of row that to be added.
        data: This is the data that have to add in the table. 

Here is the code of program:

import javax.swing.*;
import javax.swing.table.*;

public class InsertRows{
  public static void main(String[] args) {
    new InsertRows();
  }

  public InsertRows(){
    JFrame frame = new JFrame("Inserting rows in the table!");
    JPanel panel = new JPanel();
    String data[][] {{"Vinod","100"},{"Raju","200"},{"Ranju","300"}};
    String col[] {"Name","code"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    JTable table = new JTable(model);
    //Insert first position
    model.insertRow(0,new Object[]{"Ranjan","50"});
    //Insert 4 position
    model.insertRow(3,new Object[]{"Amar","600"});
    //Insert last position
    model.insertRow(table.getRowCount(),new Object[]{"Sushil","600"});
    panel.add(table);
    frame.add(panel);
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.

Output of program:

Before inserting data in the JTable

After inserting data in the JTable

                         

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:

hi

i want to detrive values from database and add tat values in JTable..... Its posssible....

how can do this process

Posted by arthi on Monday, 05.12.08 @ 12:56pm | #59440

i want to know how to insert date from jtable to my database

please provide me that code so that i can move forward to my project

Posted by kalpesh patel on Friday, 04.25.08 @ 19:07pm | #57859

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.