Source Code : Adding new row to existing Swing JTable
Adding new row to existing Swing JTable Is there an easy(ish) way to add an additional row to an existingJTable's data? I see that you can create a model of the table data using the -TableModeldataModel = theTable.getModel(); ButTableModelhas no 'insertRow()' method. I also see thatDefaultTableModelhas the insertRow() method, but I can not see a way to useDefaultTableModelwith an existing table (seems to be specific for a new table). I tried casting but you can not castTableModelas (DefaultTableModel). Is there a simple way of doing this(something like): TableModeldataModel = theTable.getModel(); dataModel.insertRow(X); theTable.setModel(dataModel); And you have the new table with end row? Vector rowData; DefaultTableModel defaultModel = new DefaultTableModel(); if (theTable.getRowCount()>0) { for (int r=0;r rowData = new Vector(6); for (int c=0;c rowData.add(theTable.getValueAt(r, c)); } defaultModel.addRow(rowData); } } defaultModel.addRow(new Object[]{"","","","","",""}); JTable newTable = new JTable(defaultModel); view plainprint? Note: Text content in the code blocks is automatically word-wrapped Vector rowData; DefaultTableModel defaultModel = new DefaultTableModel(); if (theTable.getRowCount()>0) { for (int r=0;r rowData = new Vector(6); for (int c=0;c rowData.add(theTable.getValueAt(r, c)); } defaultModel.addRow(rowData); } } defaultModel.addRow(new Object[]{"","","","","",""}); JTable newTable = new JTable(defaultModel); But at the point of [defaultModel.addRow(rowData);] whilst debugging the 'rowData' shows it has the expected values from the existing row, when I debug the 'defaultModel' the elementData shows 'nulls'. I don't know what you are doing. There is no need to copy the data from the exising model. Its one line of code. All you do is: view plainprint? Note: Text content in the code blocks is automatically word-wrapped model.addRow(...) view plainprint? Note: Text content in the code blocks is automatically word-wrapped model.addRow(...) No need to recreate the JTable. To increase your chances of getting help you should create a SSCCE, that demonstrates the incorrect behaviour. But isn't addRow() only available to DefaultTableModel? How do I use DefaultTableModel with an existing table (that already contains data). I need to be able to take an existing table and add a row to it, from what I can see an existing table can only use TableModel, which does not have the addRow() method. Did you look at the model to see what the table is currently using? If its not currently using the DefaultTableModel, then change your code when you create the table to use the DefaultTableModel. Note: Text content in the code blocks is automatically word-wrapped table = new JTable(data, ConstantValues.COLUMNS_HEADERS); view plainprint? Note: Text content in the code blocks is automatically word-wrapped table = new JTable(data, ConstantValues.COLUMNS_HEADERS); Note: Text content in the code blocks is automatically word-wrapped table = new JTable(new DefaultTableModel(data, ConstantValues.COLUMNS_HEADERS)); view plainprint? Note: Text content in the code blocks is automatically word-wrapped table = new JTable(new DefaultTableModel(data, ConstantValues.COLUMNS_HEADERS)); Then changed view plainprint? Note: Text content in the code blocks is automatically word-wrapped DefaultTableModel defaultModel = new DefaultTableModel(); view plainprint? Note: Text content in the code blocks is automatically word-wrapped DefaultTableModel defaultModel = new DefaultTableModel(); (and all that other stuff above) to: view plainprint? Note: Text content in the code blocks is automatically word-wrapped DefaultTableModel defaultModel = (DefaultTableModel) theTable.getModel(); defaultModel.addRow(new Object[]{"","","","","",""}); JTable newTable = new JTable(defaultModel); return newTable; view plainprint? Note: Text content in the code blocks is automatically word-wrapped DefaultTableModel defaultModel = (DefaultTableModel) theTable.getModel(); defaultModel.addRow(new Object[]{"","","","","",""}); JTable newTable = new JTable(defaultModel); return newTable; Haven't tested it, but early debug shows what I'd expect so I think it works.