Remove Last Row from Table Using DefaultTableModel in Java



In this article, we will learn to remove the last row from a table with DefaultTableModel in Java. Managing table data dynamically is a common requirement when working with Java Swing applications. One such operation is removing the last row from a JTable using DefaultTableModel.

Understanding DefaultTableModel in Java

DefaultTableModel is a flexible table model in Java's Swing library that allows developers to dynamically manage table data, including adding and removing rows and columns. Provides built-in methods for adding and removing rows easily.

Approach to Removing the Last Row

In this program, we create a table with multiple rows and columns using DefaultTableModel. After populating the table with sample data, we use the removeRow() method to delete the last row from the table.

Following are the steps to remove the last row from a table with DefaultTableModel in Java ?

  • Creating a Table Model: We use DefaultTableModel to define the table's structure.
  • Adding Columns: The addColumn() method is used to define four columns: Language/ Technology, Text Tutorial, Video Tutorial, and Interview QA.
  • Populating Data: We add sample rows with different technology names and their corresponding tutorial availability.
  • Removing the Last Row: The key step is using removeRow(tableModel.getRowCount() - 1) to delete the last row dynamically.
  • Displaying the Table: A JFrame with a JScrollPane is used to visualize the table.

Start by defining a DefaultTableModel and creating a JTable ?

DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);

Add some columns to the table using the addColumn() method ?

tableModel.addColumn("Language/ Technology");
tableModel.addColumn("Text Tutorial");

Adding rows to the table using add.Row() method ?

tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"});

Removing the last row from the table ?

tableModel.removeRow(tableModel.getRowCount() - 1);

Example

Below is an example of displaying rows and columns in a JTable ?

package my;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      DefaultTableModel tableModel = new DefaultTableModel();
      JTable table = new JTable(tableModel);
      tableModel.addColumn("Language/ Technology");
      tableModel.addColumn("Text Tutorial");
      tableModel.addColumn("Video Tutorial");
      tableModel.addColumn("Interview QA");
      tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"});
      table.setRowHeight(table.getRowHeight() + 10);
      JFrame f = new JFrame();
      f.setSize(600, 400);
      f.add(new JScrollPane(table));
      f.setVisible(true);
   }
}

The output is as follows. The last row here is "AWS" ?

Output

Example

Below is an example of removing the last row from a table with DefaultTableModel in Java ?

package my;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SwingDemo {
   public static void main(String[] argv) throws Exception {
      DefaultTableModel tableModel = new DefaultTableModel();
      JTable table = new JTable(tableModel);
      tableModel.addColumn("Language/ Technology");
      tableModel.addColumn("Text Tutorial");
      tableModel.addColumn("Video Tutorial");
      tableModel.addColumn("Interview QA");
      tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"});
      tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"});
      tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"});
      table.setRowHeight(table.getRowHeight() + 10);
      // remove last row from the table
      tableModel.removeRow(tableModel.getRowCount() - 1);
      JFrame f = new JFrame();
      f.setSize(600, 400);
      f.add(new JScrollPane(table));
      f.setVisible(true);
   }
}

The output now would display that we have successfully removed the last row "AWS" ?

Output

Time Complexity: for removing the last row using removeRow() is O(1).
Space Complexity: O(n), where n is the number of rows in the table.

Conclusion

Using DefaultTableModel, removing the last row from a JTable in Java is straightforward. This method ensures efficient data management while keeping the table UI updated dynamically. Whether working with small or large datasets, removeRow() provides an easy way to handle row deletions in Swing applications.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-14T18:58:58+05:30

628 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements