Why is it not recommended to use DefaultTableModel?

11

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?

    
asked by anonymous 18.01.2017 / 12:04

2 answers

1

This site (Matheus Piscioneri) summed it up very well.

  • It's harder than typing your own TableModel ;
  • It is slower (uses synchronized classes) (if you look at the code you will see that the class uses Vector and not List );
  • It takes up more memory space (duplicates your data) (I still can not test) ;
  • Leave the code more confusing and difficult to maintain (fact) ;
  • 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;

  • Make your wife leave you, the milk from your fridge sour, and people point the finger at you on the street.

A practice that ViniGodoy has always recommended.

    
23.01.2017 / 20:29
1

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:)

    
12.07.2018 / 05:04