Question

I got an AbstractTableModel, like this:

public class TableModelClienteFornecedor extends AbstractTableModel {

    private List<ClienteFornecedorDTO> linhas;
    private String[] colunas = new String[]{"Nome", "Conta"};

    public TableModelClienteFornecedor() {
        linhas = new ArrayList<>();
    }

    @Override
    public int getRowCount() {
        return linhas.size();
    }

    @Override
    public int getColumnCount() {
        return colunas.length;
    }

    @Override
    public String getColumnName(int column) {
        return colunas[column];
    }

     @Override
    public Class getColumnClass(int column) {
         return (getValueAt(0, column).getClass());        
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ClienteFornecedorDTO cf = linhas.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return cf.getNome();

            case 1:
                return cf.getConta();

            default:
                throw new IndexOutOfBoundsException("Coluna incorreta");
        }
    }

    public void clear(JTable table) {
        table.setRowSorter(null);
        int indiceAntigo = this.getRowCount();
        linhas.clear();
        int indiceNovo = this.getRowCount();
        this.fireTableRowsDeleted(indiceAntigo, indiceNovo);
    }

    public boolean isEmpty() {
        return linhas.isEmpty();
    }

    public void add(ClienteFornecedorDTO cf) {
        linhas.add(cf);
        int index = this.getRowCount();
        fireTableRowsInserted(index, index);
    }

    public void addList(List<ClienteFornecedorDTO> list, JTable table) {
        int tamanhoAntigo = this.getRowCount();
        linhas.addAll(list);
        int tamanhoNovo = this.getRowCount() - 1;
        this.fireTableRowsInserted(tamanhoAntigo, tamanhoNovo);      
        table.setAutoCreateRowSorter(true);        
    }

    public ClienteFornecedorDTO get(int i) {
        return linhas.get(i);
    }
}

And the code below works ok to fill the my Jtable with data:

private void realizarBusca(String nome) {

    IContaDAO dao = new ContaDAO();
    boolean isFornecedor = radioFornecedor.isSelected();
    List<ClienteFornecedorDTO> retorno =
         dao.retornaContaClienteFornecedor(isFornecedor, nome);
    tableModelClienteFornecedor.clear();
    tableModelClienteFornecedor.addList(retorno, tableClienteFornecedor);
    tableClienteFornecedor.updateUI();
}

Everything's working fine to me, and when I sort my Jtable visibily it's ok too, the problem is when I click on a specific row from my Jtable after I sorted it, the row it's not updated.

Anyone can help me with that? I would appreciate, 'cause I'm on it since yesterday and still wasn't able to find a way to solve it.

Was it helpful?

Solution

Look at the methods convertRowIndexToModel() and convertRowIndexToView() in JTable.

When the table is sorted, the indices of the rows in the view don't match with the indices in the model anymore, and you have to use the above methods to convert from index to view and vice-versa.

For example, if you call JTable.getSelectedRow(), you'll get the view index of the selected row. You'll have to convert it to the model index (using convertRowIndexToModel()) to be able to get the selected object from the list in your model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top