java - Javax Swing JTable::getModel vs JTable::getColumnModel -


hello have been working javax swing , came across weird problem , got me questioning. can example:

jtable table = new jtable(); // indeed, 2 different objects: // tablemodel (which, think supposed contain rows , columns? defaulttablemodel dtm = (defaulttablemodel) table.getmodel(); // , column model, supposed define columns of table? tablecolumnmodel tcm = table.getcolumnmodel();  // can add columns table in 2 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);  // , notice both commands add column in table // our table model should have 2 columns.  // it? system.out.println(dtm.getcolumncount()); // outputs 1; system.out.println(tcm.getcolumncount()); // outputs 2; system.out.println(table.getcolumncount()); // outputs 2;  // visual shows 2 columns, model has 1. 

from can tell jtable uses tablecolumnmodel , tablecolumnmodel gets columns added tablemodel, but, when add column tablemodel gets added table, tablemodel remains outdated.

now, problem is: it's interesting add column via columnmodel because can define sizes, layout, editable options there, in way cannot add data tablemodel, since column doesn't appear on tablemodel. thoughts on this?

the tablemodel used contain data. data accessible in row/columns.

the tablecolumnmodel used jtable control view of data. controls columns displayed in jtable. can reorder columns display data in different order.

...but in way cannot add data tablemodel, since column doesn't appear on tablemodel

that correct. purpose of tablecolumnmodel customize view, not manipulate data.

maybe have application contains many columns of data, access specific columns limited "security level". in case data stored in tablemodel, need change view control columns of data visible. can remove/add columns tablecolumnmodel.

when add column tablemodel, jtable gets notified , recreates tablecolumns you. can or bad thing because when tablecolumnmodel recreated lose custom renderers , editor may have added tablecolumn. can prevent happening buy using:

table.setautocreatecolumnsfrommodel( false ); 

now tablecolumnmodel not updated , responsibility manually create , add tablecolumn tablecolumnmodel.

but in general you:

  1. add/change data through tablemodel.
  2. change view through tablecolumnmodel.

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -