Javadoc says:
model.getColumnCoun() => Returns the number of columns in the model. table.getColumnModel().getColumnCount() => Returns the number of columns in this data table.
my code:
public TableEquipo(Object[][] data, String[] headers) {
super(data, headers);
model = new EquipoTableModel(data, headers);
initTable();
}
private void initTable(){
DefaultTableCellRenderer centerRenderer = new
DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
TableRowSorter<EquipoTableModel> sorter = new TableRowSorter<>(model);
sorter.setModel(model);
// System.out.println(model.getColumnCount()); out = 3
setRowSorter(sorter);
setRowHeight(30);
IntStream.range(0, getColumnCount()).forEach(
c-> getColumnModel().getColumn(c).setCellRenderer(centerRenderer));
// System.out.println(model.getColumnCount()); out = 3
setSelectionBackground(new Color(240,209,216));
// System.out.println(getColumnModel().getColumnCount()); out = 0
...
}
public EquipoTableModel getModel() {
return model;
}
In addition if i write the guetter method, getModel() the program does not load the data. why the length is different?
Assuming
model.getColumnCount()refers to a the getColumnCount method of TableModel…A TableModel returns all data available for a JTable to display. But a JTable doesn’t have to show all of those columns. It is up to the TableColumnModel to decide which columns of data will be displayed.
By default, JTable will call its own createDefaultColumnsFromModel() method to create TableColumnModel which creates displayable columns for each column in the data model. However, you can specify your own TableColumnModel which decide for itself what to display.
Here is an example of a TableModel with three columns:
And here is an example of a JTable that displays only two of those columns: