tags:

views:

221

answers:

1

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.

+1  A: 

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.

dogbane
This compiled, and showed the table, but not the column titles. It certainly is a straightforward way to get a table. I'm putting up a separate question with the code.
John R Doner
To display the columns, the easiest way is to put your table into a scroll pane:JScrollPane scroll = new JScrollPane(table);
dogbane