Table add row java

How to add row dynamically in jtable?

JTable is a powerful component in Java Swing, which provides a visual representation of tabular data. The ability to add and remove rows dynamically can be useful in many applications, but it requires a careful handling of the underlying data model. This can be achieved by using various methods such as implementing a custom TableModel, using a DefaultTableModel, or updating the underlying data source.

Method 1: Implementing Custom TableModel

To add a row dynamically in JTable, we can implement a custom TableModel. This will allow us to add and remove rows as needed.

Step 1: Create a Custom TableModel Class

import javax.swing.table.AbstractTableModel; import java.util.ArrayList; public class CustomTableModel extends AbstractTableModel  private ArrayListObject[]> data; private String[] columnNames; public CustomTableModel(ArrayListObject[]> data, String[] columnNames)  this.data = data; this.columnNames = columnNames; > @Override public int getRowCount()  return data.size(); > @Override public int getColumnCount()  return columnNames.length; > @Override public String getColumnName(int column)  return columnNames[column]; > @Override public Object getValueAt(int row, int column)  return data.get(row)[column]; > @Override public void setValueAt(Object value, int row, int column)  data.get(row)[column] = value; fireTableCellUpdated(row, column); > public void addRow(Object[] rowData)  data.add(rowData); int row = data.size() - 1; fireTableRowsInserted(row, row); > public void removeRow(int row)  data.remove(row); fireTableRowsDeleted(row, row); > >

Step 2: Create JTable and CustomTableModel Object

String[] columnNames = "ID", "Name", "Age">; ArrayListObject[]> data = new ArrayListObject[]>(); CustomTableModel tableModel = new CustomTableModel(data, columnNames); JTable table = new JTable(tableModel);

Step 3: Add Row to JTable

Object[] rowData = 1, "John", 30>; tableModel.addRow(rowData);

Step 4: Remove Row from JTable

int selectedRow = table.getSelectedRow(); if (selectedRow != -1)  tableModel.removeRow(selectedRow); >

In this example, we have created a custom TableModel class that extends AbstractTableModel. This allows us to add and remove rows from the JTable dynamically. We have also created a JTable object and passed the CustomTableModel object to it. Finally, we have added and removed rows from the JTable using the CustomTableModel object.

Method 2: Using DefaultTableModel

To add a row dynamically in a JTable using DefaultTableModel, follow these steps:

DefaultTableModel model = new DefaultTableModel(); JTable table = new JTable(model);
model.addColumn("Column 1"); model.addColumn("Column 2");
  1. To add a row dynamically, use the addRow() method of the DefaultTableModel class. This method takes an array of objects that represent the values of each column in the row.
Object[] row = "Value 1", "Value 2">; model.addRow(row);
  1. You can also insert a row at a specific position using the insertRow() method. This method takes two parameters: the index at which to insert the row and an array of objects representing the values of each column in the row.
Object[] row = "Value 1", "Value 2">; model.insertRow(0, row);
  1. To remove a row from the table, use the removeRow() method. This method takes the index of the row to remove as its parameter.

Here’s an example that puts it all together:

DefaultTableModel model = new DefaultTableModel(); JTable table = new JTable(model); model.addColumn("Column 1"); model.addColumn("Column 2"); Object[] row1 = "Value 1", "Value 2">; Object[] row2 = "Value 3", "Value 4">; model.addRow(row1); model.addRow(row2); Object[] newRow = "Value 5", "Value 6">; model.addRow(newRow); model.insertRow(0, newRow); model.removeRow(2);

This code creates a JTable with two columns, adds two rows to it, inserts a new row at the beginning, adds another row to the end, and then removes the third row.

Method 3: Updating Underlying Data Source

To add a row dynamically in a JTable using the «Updating Underlying Data Source» approach, you need to follow these steps:

  1. Get the table model from the JTable object.
  2. Add a new row to the underlying data source.
  3. Fire a table row insertion event to notify the table model of the new row.
  4. Update the JTable to show the new row.
DefaultTableModel model = (DefaultTableModel) jTable.getModel(); Object[] newRow = "New Data 1", "New Data 2", "New Data 3">; model.addRow(newRow); int row = model.getRowCount() - 1; jTable.setRowSelectionInterval(row, row); jTable.scrollRectToVisible(jTable.getCellRect(row, 0, true));

In this example, we first get the table model from the JTable object using the getModel() method. Then, we create a new row with the data we want to add. Next, we add the new row to the underlying data source using the addRow() method of the table model. After that, we get the index of the new row using the getRowCount() method and subtracting 1 to get the index of the last row. We then select the new row using the setRowSelectionInterval() method and scroll the table to show the new row using the scrollRectToVisible() method.

That’s it! Now you know how to add a row dynamically in a JTable using the «Updating Underlying Data Source» approach.

Источник

Читайте также:  Html img input type
Оцените статью