How to remove all rows from a Java JTable created using Custom TableModel

To remove all rows from a Java JTable created using Custom TableModel, try the following:

MyModel myTableModel = (MyModel) myTable.getModel(); for (int i = myTableModel.getRowCount()-1; i >= 0; i–) myTableModel.deleteRow(i);

Define the deleteRow(int row) method in the MyModel class.

Read data from a JTable created from AbstractTableModel

If you have a JTable in Java created with an AbstractTableModel, you can read the data from it, using following:

int rowCount = testTable.getRowCount(); for (int i = 0; i < rowCount; i++) { String mColumn1 = (String) testTable.getModel().getValueAt(i, 0); String mColumn2 = (String) testTable.getModel().getValueAt(i, 1); }

Alternatively, you could also try where the JTable . . . → Read More: Read data from a JTable created from AbstractTableModel

Delete a selected row in JTable with a custom model

DefaultTableModel model = (DefaultTableModel) myTable.getModel(); //Get reference to model model.removeRow(x); //Remove row x

You can also add a method deleteRow(int row) to your custom model class code as in:

public class MyTableModel extends AbstractModel { private Vector dataVector = new Vector(); String[] columnNames = {“First Name”, “Last Name”, “Age”}; public int getRowCount() { if (dataVector.size() . . . → Read More: Delete a selected row in JTable with a custom model

Delete all rows from a Java JTable with a custom TableModel

I want to clear all rows in a custom TableModel. Unfortunately, a custom TableModel in Java does not allow use of most efficient of row clearing methods, which only applies to a DefaultTableModel like that shown below: myTableModel.getDataVector().removeAllElements(); myTableModel.fireTableDataChanged(); To do an all clear like the above, for a custom TableModel, you need to reset . . . → Read More: Delete all rows from a Java JTable with a custom TableModel

How to print a date in the default JTable.print() header and setup the page

After a struggle, managed to figure out that the MessageFormat is not that complicated at all. All I wanted was to print a heading on each page of the JTable with the current date in it. The heading was to read “Report as on ” + today’s date.

Here is how I accomplished the task. . . . → Read More: How to print a date in the default JTable.print() header and setup the page

September 2019
M T W T F S S
« Aug    
 1
2345678
9101112131415
16171819202122
23242526272829
30