Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Removing a Row from a JTable 
 

For removing the data of row from JTable, you will remove it from its table model.

 

Removing a Row from a JTable

                         

After inserting the data in a JTable, if you wish to remove any one row's data that is wrong entry then you must have to remove from the JTable. For removing the data of row from JTable, you will remove it from its table model. An implementation of DefaultTableModel that supports the removal data of row.  Here providing you an example with code that removes some rows from a table.

Description of program:

This program creates a table with some data by using the DefaultTableModel that helps us for removing the data of row from a table. The removeRow() method is used to remove data at the specified index of row that have to removed from a table. Here the data of first and last rows are deleted by using the removeRow method. The following output will represent to it so, you will easily understood about it.

Description of code:

removeRow( int row_index ):
This method is used to remove the row from the model. It takes integer type row index that means its position or location in a table.

    row_index: This is the index of row that have to removed.

Here is the code of program:

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

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

  public RemoveRows(){
    JFrame frame = new JFrame("Inserting rows in the table!");
    JPanel panel = new JPanel();
    String data[][] {{"Vinod","100"},{"Raju","200"},{"Ranju","300"},
     {
"Rahul","400"},{"Noor","600"}};
    String col[] {"Name","code"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    JTable table = new JTable(model);
    System.out.println("Befoure removing, number of rows: " 
          
+ model.getRowCount());
    //Remove first row
    model.removeRow(0);
    System.out.println("After removing first row, number of rows: " 
           + model.getRowCount
());
    //Remove lase row
    model.removeRow(table.getRowCount()-1);
    System.out.println("After removing last row, number of rows: " 
         
+ table.getRowCount());
    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 removing rows:

After removing rows:

                         

» View all related tutorials
Related Tags: c ide class methods method get icon height vi width change using int id ai create js define show paint

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 

Current Comments

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

Hi,
This code is useful, but suppossing I am choosing a random value for the removal criteria and when the table is connected to a database.
Then how do i modify this code.
This is Urgent!!!
Thanks

Posted by Shivram on Thursday, 05.15.08 @ 13:00pm | #60015

plz give me code of span two cells of table
plz

Posted by arun on Thursday, 12.20.07 @ 12:13pm | #43085

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.