The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Rows  [8 examples]

e910. Removing a Row from a JTable Component

To remove a row of data from a JTable component, you need to remove it from its table model. A simple implementation of a table model that supports the removal of row data is DefaultTableModel.

When removing a row using DefaultTableModel.removeRow(), the index of the row must be specified. Row indices start from 0. For example, if there are 2 rows in a table, the index of the first row is 0 and the index of the second row is 1. Removing a row at index 0 removes the first row.

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    
    // Create some data
    model.addColumn("Col1");
    model.addRow(new Object[]{"r1"});
    model.addRow(new Object[]{"r2"});
    model.addRow(new Object[]{"r3"});
    
    // Remove the first row
    model.removeRow(0);
    
    // Remove the last row
    model.removeRow(model.getRowCount()-1);

 Related Examples
e907. Getting the Number of Rows and Columns in a JTable Component
e908. Appending a Row to a JTable Component
e909. Inserting a Row in a JTable Component
e911. Moving a Row in a JTable Component
e912. Copying a Row or Column in a JTable Component
e913. Setting the Height of a Row in a JTable Component
e914. Shading Rows and Columns in a JTable Component

See also: Cells    Column Heads    Columns    Editing    Events    Layout    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.