I looked at some sites on how to fill a table in java, and many were suggested to avoid DefaultTableModel
?
Why should you avoid using this class for cases of more complex objects? What does it imply in your use?
I looked at some sites on how to fill a table in java, and many were suggested to avoid DefaultTableModel
?
Why should you avoid using this class for cases of more complex objects? What does it imply in your use?
This site (Matheus Piscioneri) summed it up very well.
Vector
and not List
); Use insecure casts (for example in method convertToVector(Object[] anArray)
where: Vector<Object> v = new Vector<Object>(anArray.length);
for (Object o : anArray) {
v.addElement(o);
};
Force you to display unnecessary information (such as ID) in the table, or control the ID in a separate list;
A practice that ViniGodoy has always recommended.
Actually, using DefaultTable is more complicated. And maintenance tends to be more time-consuming as well.
When I was seeing about it, I used this example and it helped me to understand very well.
Maybe I can help you.
package nome_do_pacote;
import br.com.agenda.modelo.*;
import java.util.*;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class ContatoTableModel extends AbstractTableModel{
public static final int COLUNA_CODIGO = 0;
public static final int COLUNA_NOME = 1;
public static final int COLUNA_EMAIL = 2;
public static final int COLUNA_FIXO = 3;
public static final int COLUNA_CELULAR = 4;
private List<Contato> listarContatos;
public ContatoTableModel(List<Contato> l){
this.listarContatos = new ArrayList<Contato>(l);
}
public int getRowCount(){
return listarContatos.size();
}
public int getColumnCount(){
return 5;
}
public Object getValueAt(int linhas, int colunas){
Contato cont = this.listarContatos.get(linhas);
if(colunas == COLUNA_CODIGO) return cont.getCodigo();
if(colunas == COLUNA_NOME) return cont.getNome();
if(colunas == COLUNA_EMAIL) return cont.getEmail();
if(colunas == COLUNA_FIXO) return cont.getFixo();
if(colunas == COLUNA_CELULAR) return cont.getCelular();
return "";
}
public String getColumnName(int colunas){
if(colunas == COLUNA_CODIGO) return "Código";
if(colunas == COLUNA_NOME) return "Nome";
if(colunas == COLUNA_EMAIL) return "E-mail";
if(colunas == COLUNA_FIXO) return "Telefone";
if(colunas == COLUNA_CELULAR) return "Celular";
return "";
}
public Contato get(int linhas){
return listarContatos.get(linhas);
}
}
DefaultTable is on Oracle's website, if it was that bad, it would not be there. For those who are beginner, I find it simple to use using IDE as netbeans for example.
No fanaticism:)