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.
Tag: JTable
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 … Continue reading 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() … Continue reading 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 … Continue reading 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. … Continue reading How to print a date in the default JTable.print() header and setup the page