Arpit Porwal
Arpit Porwal

Reputation: 293

Insert a new row in jTable automatically while entering data in the last row

What I have done til now?

I've created a business application which needs the information to be added with the help of a jTable. Below is the image of my jTable.enter image description here

The '+' button here adds a new row while '-' button removes the selected row on clicking the respective buttons.

What I need?

I need an idea about any event or something which insert a new row after the last row while the user starts adding data to the last row at runtime.

Specifically, I don't need any button event to add rows. The row must be automatically inserted while the last row is not empty anymore.

Please Help!

Upvotes: 1

Views: 2087

Answers (1)

cello
cello

Reputation: 5486

You could try it with a TableModelListener. Basic idea: you get notified, whenever a cell was edited. You can then check if the cell was in the last row, and if it was, add a new row to the table.

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

tableModel.addTableModelListener(new TableModelListener() {
  @Override
  public void tableChanged(TableModelEvent e) {
    if ((e.getLastRow() + 1) == tableModel.getRowCount()) {
      // something was entered into the last row, add a new row
    }
  }
});

Upvotes: 2

Related Questions