Why is it not recommended to use Defaulttablemodel?

Asked

Viewed 490 times

12

I searched some sites about how to fill a table in java and many suggested to avoid the DefaultTableModel?

Why should one avoid using this class for more complex object cases? What does its use imply?

2 answers

2

This website (Matheus Piscioneri) summarized the reasons very well.

  • It’s harder than writing your own Tablemodel (fact);
  • It is slower (uses synchronized classes) (if you look at the code you will see that the class uses Vector and not List);
  • Takes up more memory space (duplicates your data) (I haven’t been able to test) ;
  • Makes code more confusing and difficult to maintain (fact);
  • Uses insecure cases (for example in the method convertToVector(Object[] anArray) where : Vector<Object> v = new Vector<Object>(anArray.length);

    for (Object o : anArray) {
        v.addElement(o);
    };
    
  • Force that you have to display unnecessary information (such as the ID) in the table, or control the ID in a separate list;

  • Make your wife let you, the milk in your fridge goes sour, and people point the finger at you on the street. (This really happened);

A practice that Vinigodoy always recommended.

2

Actually, the use of Defaulttable is more complicated. And maintenance ends up being more time consuming too.

When I was looking at it, I used this example and it helped me understand it 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’s on the Oracle website, if it was that bad, it wouldn’t be there. For beginners, I find it simple to use using IDE like netbeans for example.

Fanaticism-free :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.