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:
public record Country(String name,
String iso3166Code,
long population) { }
public class CountryModel
extends AbstractTableModel {
private static final long serialVersionUID = 1;
public static final int COLUMN_NAME = 0;
public static final int COLUMN_CODE = 1;
public static final int COLUMN_POPULATION = 2;
public static final int NUM_COLUMNS = 3;
private final List<Country> countries = new ArrayList<>();
public CountryModel() {
// Deliberately empty.
}
public CountryModel(Collection<Country> countries) {
addAll(countries);
}
public Country getDataAt(int rowIndex) {
return countries.get(rowIndex);
}
public void add(Country country) {
countries.add(Objects.requireNonNull(country,
"Country cannot be null"));
}
public void addAll(Collection<Country> countries) {
Objects.requireNonNull(countries, "Argument cannot be null.");
if (countries.stream().anyMatch(Objects::isNull)) {
throw new IllegalArgumentException(
"Argument cannot contain null.");
}
this.countries.addAll(countries);
}
@Override
public int getRowCount() {
return countries.size();
}
@Override
public int getColumnCount() {
return NUM_COLUMNS;
}
@Override
public String getColumnName(int columnIndex) {
return switch (columnIndex) {
case COLUMN_NAME -> "Name";
case COLUMN_CODE -> "Code";
case COLUMN_POPULATION -> "Population";
default ->
throw new IllegalArgumentException(
"Invalid column: " + columnIndex);
};
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex == COLUMN_POPULATION ? Long.class : Object.class;
}
@Override
public Object getValueAt(int rowIndex,
int columnIndex) {
Country country = countries.get(rowIndex);
return switch (columnIndex) {
case COLUMN_NAME -> country.name();
case COLUMN_CODE -> country.iso3166Code();
case COLUMN_POPULATION -> country.population();
default ->
throw new IllegalArgumentException(
"Invalid column: " + columnIndex);
};
}
}
And here is an example of a JTable that displays only two of those columns:
TableModel dataModel = new CountryModel(
List.of(
new Country("United States", "US", 333_287_557),
new Country("United Kingdom", "GB", 66_971_411),
new Country("Canada", "CA", 40_097_761),
new Country("Australia", "AU", 26_910_600)
));
TableColumnModel columnModel = new DefaultTableColumnModel();
columnModel.addColumn(new TableColumn(CountryModel.COLUMN_NAME));
columnModel.addColumn(new TableColumn(CountryModel.COLUMN_POPULATION));
// COLUMN_CODE deliberately omitted.
for (int i = columnModel.getColumnCount() - 1; i >= 0; i--) {
TableColumn c = columnModel.getColumn(i);
String name = dataModel.getColumnName(c.getModelIndex());
c.setHeaderValue(name);
}
Box labels = Box.createVerticalBox();
labels.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
labels.add(new JLabel(
"TableModel columnCount: " + dataModel.getColumnCount()));
labels.add(Box.createVerticalStrut(6));
labels.add(new JLabel(
"ColumnModel columnCount: " + columnModel.getColumnCount()));
JTable table = new JTable(dataModel, columnModel);
JFrame frame = new JFrame("Countries");
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
frame.getContentPane().add(labels, BorderLayout.PAGE_END);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);