Felype
Felype

Reputation: 3136

Javax Swing JTable::getModel vs JTable::getColumnModel

Hello I have been working with javax swing and I came across a weird problem and got me questioning. I can for example:

JTable table = new JTable();
// Indeed, 2 different objects:
// The TableModel (which, i think is supposed to contain rows and columns?
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
// And the column model, supposed to define the columns of a table?
TableColumnModel tcm = table.getColumnModel();

// I can add columns to my table in two different manners
dtm.addColumn("A Column");
// or
TableColumn column = new TableColumn();
column.setHeaderValue("Another column");
column.setWidth(120);
column.setMinWidth(120);
column.setMaxWidth(120);
tcm.addColumn(column); 
// And notice that both commands will add a column in the table
// So our table model should now have 2 columns.

// But does it?
System.out.println(dtm.getColumnCount()); // outputs 1;
System.out.println(tcm.getColumnCount()); // outputs 2;
System.out.println(table.getColumnCount()); // outputs 2;

// The visual shows 2 columns, but the model has only 1.

From that I can tell JTable uses tableColumnModel and tableColumnModel gets all the columns added into tableModel, but, when I add a column to the TableModel it gets added to the table, but the tableModel remains outdated.

Now, the problem is: it's really interesting to add a column via columnModel because I can define the sizes, layout, editable options there, but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel. Any thoughts on this?

Upvotes: 1

Views: 2033

Answers (1)

camickr
camickr

Reputation: 324118

The TableModel is used to contain data. The data is accessible in row/columns.

The TableColumnModel is used by JTable to control the View of the data. That is it controls the columns that are displayed in the JTable. You can also reorder the columns to display the data in a different order.

...but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel

That is correct. The purpose of the TableColumnModel is to simply customize the view, not manipulate data.

Maybe you have an application that contains many columns of data, but access to specific columns is limited by "security level". In this case the data is always stored in the TableModel, but you need to change the view to control which columns of data are visible. So you can remove/add columns from the TableColumnModel.

When you add a column to the TableModel, the JTable gets notified and it recreates all the TableColumns for you. This can be a good or bad thing because when the TableColumnModel is recreated you lose any custom renderers and editor that you may have added to a TableColumn. You can prevent this from happening buy using:

table.setAutoCreateColumnsFromModel( false );

Now the TableColumnModel will not be updated and it is your responsibility to manually create and add the TableColumn to the TableColumnModel.

But in general you:

  1. add/change data through the TableModel.
  2. change the view through the TableColumnModel.

Upvotes: 4

Related Questions