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<Depto> deptos;
public DeptoTableModel(){
deptos=new ArrayList<Depto>();
}
public DeptoTableModel(List<Depto>listas) {
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());