Append a Row to JTable in Java Swing



Adding rows dynamically to a JTable is a common task in Java Swing when working with user interfaces that involve tabular data. This article walks you through creating a Java Swing application that appends a new row to a JTable when the user inputs data and clicks a button.

What is JTable?

A JTable is a powerful Swing component for displaying and editing tabular data. By using a DefaultTableModel, we can manage the table's data dynamically, including adding or removing rows. This makes it an excellent choice for applications requiring user interaction.

Approach

Following are the steps to append a row to a JTable in Java Swing ?

  • Create a Table Model: Use DefaultTableModel to define the table's columns and manage its data.
  • Define Columns: Add two columns: "Language/Technology" and "Difficulty Level".
  • Insert Initial Rows: Use insertRow(0, new Object[]{...}) to insert rows at the top. Rows are displayed in reverse order of insertion.
  • Add New Rows: Append a row using insertRow(tableModel.getRowCount(), ...) or addRow(...).
  • Create and Add JTable: Initialize JTable with the table model. Add it to a JScrollPane for scrollability.
  • Display in JFrame: Create a JFrame, set its size, and add the scroll pane containing the JTable.

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("Difficulty Level");

Add some initial rows to the table using the insertRow() method ?

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

Finally, to append a row to the table, use the addRow() method ?

tableModel.addRow(new Object[] { "WordPress", "Easy" });

Example

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

Output

Time Complexity: for the row insertion O(n), for the row append O(1), for the table rendering O(n×m).
Space Complexity: O(n×m), for table data storage.

Conclusion

Appending rows to a JTable dynamically is straightforward with DefaultTableModel and addRow(). This approach is ideal for creating interactive applications where users can add, edit, or manage tabular data. By integrating features like real-time updates and error handling, you can enhance the user experience effectively.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-01-17T19:44:35+05:30

980 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements