Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate an empty JTable?

Tags:

swing

jtable

I am climbing the java learning curve, and for the first time I need a JTable. What I would like to do is display an empty table, with all cells vacant except for column headings. Then as a result of user actions, the tables is filled with a mix of strings, integers, and floats.

All the examples I find on the web create tables that are populated at instantiation. Is there any simple way to defer populating the table, but displaying it at startup?

Thanks in advance for any help.

like image 239
John R Doner Avatar asked Dec 10 '25 06:12

John R Doner


1 Answers

Create a table model with the specified number of rows and columns and use that for your JTable. For example:

String[] colHeadings = {"COLUMN1","COLUMN2"};
int numRows = 5 ;
DefaultTableModel model = new DefaultTableModel(numRows, colHeadings.length) ;
model.setColumnIdentifiers(colHeadings);
JTable table = new JTable(model);

You can then call methods on the model to update values, add rows etc.

like image 155
dogbane Avatar answered Dec 12 '25 20:12

dogbane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!