Problema com AbstractTableModel [RESOLVIDO]

2 respostas
R

Vendo os TableModels implementados, me interessei e implementei um para teste.
Porem ao executar o Jframe o Jtable esta listando as linhas conforme o nome das colunas passadas no TableModel.

Como nao tenho muito conhecimento dessa classe pesso ajuda para executar pois parece mais funcional do que o DefaultTableModel.

TableModel

[/code]public class DeptoTableModel extends AbstractTableModel{

private static final long serialVersionUID=1L;

private static final int COL_CD_DEPTO = 0;
private static final int COL_DS_DEPTO = 1;

private List deptos;

public DeptoTableModel(){
deptos=new ArrayList();

}

public DeptoTableModel(Listlistas) {
this();
this.deptos.clear();
this.deptos.addAll(listas);
super.fireTableDataChanged();

}

public int getRowCount() {
return deptos.size();
}

public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int column){

if(column == COL_CD_DEPTO) return "cd_depto";
if(column == COL_DS_DEPTO) return "ds_depto";
return"";
}

@Override
public Object getValueAt(int row, int column) {

Depto depto=deptos.get(row);
if(column==COL_CD_DEPTO) return "cd_depto";
else if(column==COL_DS_DEPTO) return "ds_depto";
return"";
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

super.setValueAt(aValue, rowIndex, columnIndex);
}

@Override
public Class getColumnClass(int coluna) {
return String.class;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex){

return true;
}

public Depto get(int row){
return deptos.get(row);
}

}

ArrayList
public ArrayList<Depto> listarDepto() throws SQLException {

        PreparedStatement stmt = connection.prepareStatement("select cd_depto,ds_depto from depto");

        ResultSet rs = stmt.executeQuery();

        ArrayList <Depto> listaDepto = new ArrayList<Depto>();

        
        while(rs.next()) {
            Depto depto;
            depto = new Depto();
            depto.setCd_depto(rs.getString("cd_depto"));
            depto.setDs_depto(rs.getString("ds_depto"));
            listaDepto.add(depto);
 
        }
        
        return listaDepto;


    }

gettableModel
[/code]

private TableModel getTableModelDeptos() throws Exception{

DeptoDAO dao = new DeptoDAO();
return new DeptoTableModel(dao.listarDepto());
}
[code]

Setando o Modelo

jTable1.setModel(getTableModelDeptos());

2 Respostas

ViniGodoy

Oi, quando postar código, por favor, use a tag code:
[code]
Seu código aqui
[/code]

Seu problema é que seu método getValueAt deveria retornar para o JTable o que ele vai pintar nas células. Entretanto, o que você está retornando? O título das colunas. Então, todas as células serão pintadas com o título das colunas. O correto será mais ou menos assim:

@Override
public Object getValueAt(int row, int column) {
   Depto depto=deptos.get(row);
   if(column==COL_CD_DEPTO) return depto.getCd();
   else if(column==COL_DS_DEPTO) return depto.getDs();
   return"";
}
R

ViniGodoy, carregou a jtable certim … Vlw!

Criado 18 de maio de 2010
Ultima resposta 18 de mai. de 2010
Respostas 2
Participantes 2