Add New Row to JTable with insertRow in Java Swing



The JTable component in Java Swing is widely used for displaying and managing tabular data. Adding a new row dynamically to a JTable can enhance user interactivity, especially in applications that require real-time data manipulation. In this article we will tell you a step-by-step procedure, to use the insertRow() method to add a new row to a JTable, complete with a sample implementation and an algorithm.

What is JTable in Java Swing?

JTable is a Swing component used for displaying and editing tabular data. It is highly customizable and supports features like sorting, selection, and dynamic data manipulation, making it an essential tool for GUI-based applications.

insertRow() method  

The HTML DOM table insertRow() method generates an empty element and adds it to the table in an HTML document.

Dynamically Adding Rows to JTable

To dynamically add a row to a JTable ?

  • JTable data is managed by a TableModel.
  • Use a mutable table model like DefaultTableModel to allow modifications.
  • The insertRow(int row, Object[] rowData) method is used to insert a new row at a specified index.

Let us first create a table with DefaulTabelMode ?

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

Now, add a column to the table ?

tableModel.addColumn("Languages");

The insertRow() method will now add a row ?

tableModel.insertRow(0, new Object[] { "CSS" });
tableModel.insertRow(0, new Object[] { "HTML5" });
tableModel.insertRow(0, new Object[] { "JavaScript" });
tableModel.insertRow(0, new Object[] { "jQuery" });

Example

The following is an example to add a new row to 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("Languages");
      tableModel.insertRow(0, new Object[] { "CSS" });
      tableModel.insertRow(0, new Object[] { "HTML5" });
      tableModel.insertRow(0, new Object[] { "JavaScript" });
      tableModel.insertRow(0, new Object[] { "jQuery" });
      tableModel.insertRow(0, new Object[] { "AngularJS" });
      // adding a new row
      tableModel.insertRow(tableModel.getRowCount(), new Object[] { "ExpressJS" });
      JFrame f = new JFrame();
      f.setSize(550, 350);
      f.add(new JScrollPane(table));
      f.setVisible(true);
   }
}

Output

Time Complexity: Adding or inserting a row in DefaultTableModel is O(n), where n is the number of rows, as it may involve shifting rows to accommodate the new one.

Space Complexity: O(1), as the operation does not require additional space beyond the existing structure.

Advantages of Using insertRow() in JTable n

  • Dynamic Data Updates: Rows can be added at runtime, enhancing interactivity.
  • Custom Index: Allows insertion at any position, not just at the end.
  • Built-in Refresh: Automatically updates the JTable view without additional steps.

Conclusion

Adding a new row to a JTable using insertRow() is a straightforward process when leveraging the DefaultTableModel class. This method provides the flexibility to update the JTable dynamically, catering to real-time user interactions. By following the algorithm and example provided, you can seamlessly integrate row addition functionality into your Java Swing

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-24T17:46:46+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements