How to instantiate an empty JTable?

I climb the java learning curve and for the first time I need a JTable. What I would like to do is show an empty table, with all the cells being empty except for the column headers. Then, as a result of user actions, the tables are filled with a combination of strings, integers, and floating-point numbers.

All the examples that I find on the Internet create tables that populate when the instance is created. Is there an easy way to postpone filling the table, but show it at startup?

Thanks in advance for your help.

+3
source share
2 answers

Create a table model with the specified number of rows and columns and use it for JTable. For instance:

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

, ..

+6

, , ...

, . null, 4 4 , .

jTable1.setModel(new DefaultTableModel(
   new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                   }, new String [] {"Title 1", "Title 2", "Title 3", "Title 4"}
                ));

/ ...

jTable1.setValueAt(Object data, int row, int column);

, . .

0

Source: https://habr.com/ru/post/1734003/


All Articles