Remove First Row from Table with DefaultTableModel in Java



In this article, we will learn how to display a table with multiple rows and columns in Java using the JTable component. Additionally, we will demonstrate how to remove the first row from the table using the removeRow() method. The first program shows how to create and display a table with 9 rows, and the second program demonstrates how to remove the first row, leaving the remaining 8 rows intact. 

Create a table with rows and columns using JTable

Following are the steps to create a table with rows and columns using JTable ?

  • Start by importing in the necessary classes like JFrame, JTable, JScrollPane, and DefaultTableModel from the javax.swing package to work with the GUI components.
  • Create a table model by initializing a DefaultTableModel, which will help us manage the data that will go into our table.
  • Create a JTable object and link it to the DefaultTableModel we just set up. This connects our data model to the table display.
  • We will add the columns by using the addColumn() method to create four columns. We'll name them "Language/ Technology," "Text Tutorial," "Video Tutorial," and "Interview QA" to categorize our data clearly.
  • Add nine rows of data using the addRow() method. Each row will contain different technology-related information.
  • Call setRowHeight() to increase the height of each row. This makes the text easier to read and improves the overall look of the table.
  • Create a JFrame, add the table wrapped in a JScrollPane to allow for scrolling if needed, and then make the frame visible to show our table to the user.

Example

Let us first see an example to display a table with rows and columns. The table here has 9 rows ?

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);
   }
}

Output

The output is as follows. The first row is "Blockchain" ?

Removing the first row from the JTable

Following are the steps to remove the first row from the JTable ?

  • Import the same classes as in the previous program, including JFrame, JTable, JScrollPane, and DefaultTableModel from the javax.swing package to handle our GUI components.
  • Set up a new DefaultTableModel. This will allow us to manage the data for our table just like before.
  • Create a JTable object and add the same four columns to it: "Language/ Technology," "Text Tutorial," "Video Tutorial," and "Interview QA." This keeps our data organized.
  • Add the same nine rows of technology-related data using the addRow() method, ensuring consistency with the previous example.
  • Call removeRow(0) on the DefaultTableModel to remove the first row from the table. This updates our table to display only the remaining rows.
  • Create a JFrame, add the modified table inside a JScrollPane to allow scrolling, and then set the frame to visible to show the updated table to the user.

Example

In the given program we will remove the first row. After removing the total number of rows from the above table would be 8 ?

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 first row from the table
      tableModel.removeRow(0);
      JFrame f = new JFrame();
      f.setSize(600, 400);
      f.add(new JScrollPane(table));
      f.setVisible(true);
   }
}

Output

The output now would display that we have successfully removed the first row and rest of the 8 rows would be visible ?

Updated on: 2024-10-23T17:30:38+05:30

719 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements