my question is how to set dynamic, the number of rows and columns in a JTable? I mean if the user wants to create a table of 2 rows and 2 columns, he just type down the number. How can I do that, I've tried with the DefaultModel with no success.
I will appreciate any help.
Thanks
DefaultTableModel
has two means by which you can define the number of rows/columns at runtime.You could...
Simply create a new
DefaultTableModel
, passing it the rows and columns you want...and then apply this to the
JTable
. This will, obviously, replace the existing table model, meaning you will lose all of it's data.You could...
Create an master
DefaultTableModel
and apply it to theJTable
and simply useto dynamically update the number of rows and columns as required. This will allow you to preserve the data within the table model (expect when you remove rows or columns, then it's lost)
Runnable example
For more details, see...