Question

I am wanting to add a blank table with headers to a miniature program I am working on. Obviously this is because part of the program's purpose is to add information to the table. I need the column's to have headers which I have read up on how to do but I am unable to figure out how to define a table without coding in content.

    String[] ColumnNames = {"1column", "2column", "3column"};
/*
*
*/
        EmptyTable = new JTable((???),ColumnNames);
        add(EmptyTable);

This is the code I currently have. The (???) is there because I am strongly suspecting the missing code I cannot find is to belong in that spot.

Was it helpful?

Solution

The first parameter of that JTable constructor takes an Object[][], expected to be the initial table model.

emptyTable = new JTable(new Object[ColumnNames.length][], columnNames);

As a side note, variables should be named with camelCase, as is dictated by Java conventions (i.e. EmptyTable should be emptyTable, ColumnNames should be columnNames).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top