Home Java Example Java Swing Removing a Row from a JTable
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Removing a Row from a JTable

Advertisement
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:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Removing a Row from a JTable   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
jamaica
July 6, 2011
thank you!

Thank you very much! I needed this! now I am one step closer to finishing my program :3
DMCA.com