wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Adding new row to existing Swing JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding new row to existing Swing JTable" Watch "Adding new row to existing Swing JTable" New topic
Author

Adding new row to existing Swing JTable

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi there

Is there an easy(ish) way to add an additional row to an existing JTable's data?

I see that you can create a model of the table data using the
- TableModel dataModel = theTable.getModel();

But TableModel has no 'insertRow()' method.

I also see that DefaultTableModel has the insertRow() method, but I can not see a way to use DefaultTableModel with an existing table (seems to be specific for a new table). I tried casting but you can not cast TableModel as (DefaultTableModel).

Is there a simple way of doing this(something like):

TableModel dataModel = theTable.getModel();
dataModel.insertRow(X);
theTable.setModel(dataModel);

And you have the new table with end row?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You can always create your JTable using a DefaultTableModel right from the start, and then use that model to add rows. It's simple and easy to do.

Best of luck!
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
I've got as far as:


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'.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
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:



No need to recreate the JTable.

To increase your chances of getting help you should create a SSCCE, that demonstrates the incorrect behaviour.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
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.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
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.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Ah!

I had


changed it


Then changed


(and all that other stuff above) to:



Haven't tested it, but early debug shows what I'd expect so I think it works.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding new row to existing Swing JTable
 
Similar Threads
Trouble with model for calculating and adding table values dependent on multiple rows
adding rows to table dynamically
add an empty row to the Jtable
Add a new row to JTable
Dynamic JTable