I have a subclass of AbstractTableModel
and a JFrame
to display the data from my table, ran over and the only error that appears instead of the column names appear A, B, C
What am I doing wrong?
Here are my classes
package Biblioteca;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TabelaAlunos extends AbstractTableModel {
private ArrayList linhas = null;
private String[] colunas = {"id_aluno", "nome_aluno", "matricula", "telefone", "email", "sexo"};
public TabelaAlunos(ArrayList lin, String[] col) {
setColunas(col);
setLinhas(lin);
}
public ArrayList getLinhas() {
return linhas;
}
public void setLinhas(ArrayList dados) {
linhas = dados;
}
public String[] getColunas() {
return colunas;
}
public void setColunas(String[] nomes) {
colunas = nomes;
}
@Override
public int getColumnCount() {
return colunas.length;
}
@Override
public int getRowCount() {
return linhas.size();
}
@Override
public String getColumnName(int columnIndex) {
return colunas[columnIndex];
}
@Override
public Object getValueAt(int numLin, int columnIndex) {
Object[] linha = (Object[]) getLinhas().get(numLin);
return linha[columnIndex];
}
};
There is a typo in this method:
It should be (note the u instead of o):
This is why
@Override
annotation is important when we are subclassing and overriding methods. If you include this annotation in your actual code it shouldn't compile becausegetColomnName(...)
is not defined in parent class.The same principle applies for all these methods:
getColumnCount()
getRowCount()
getValueAt(int row, int column)
Edit
Based on your update your table model looks just fine. I've made an MCVE using your table model and it all worked as expected. Check out the questions' third revision, you don't use
TabelaAlunos
table model butTabelaLivros
instead, so it's probably the issue is in that table model.You might also consider wrap your data using POJO's to model business data and implement a table model like exemplified here. There are advanced alternatives shown here and here. Also see Table From Database by Rob Camick.
Finally, please see the example below:
Screenshot