Java 排序后无法从JTable中获取正确的行(Swing)

Java 排序后无法从JTable中获取正确的行(Swing),java,swing,netbeans,jtable,abstracttablemodel,Java,Swing,Netbeans,Jtable,Abstracttablemodel,我得到了一个AbstractTableModel,如下所示: public class TableModelClienteFornecedor extends AbstractTableModel { private List<ClienteFornecedorDTO> linhas; private String[] colunas = new String[]{"Nome", "Conta"}; public TableModelClienteForn

我得到了一个AbstractTableModel,如下所示:

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);
    }
}
公共类TableModelClientForNeceDor扩展AbstractTableModel{
私人名单;
私有字符串[]colunas=新字符串[]{“Nome”,“Conta”};
public table model clientefornecedor(){
linhas=newarraylist();
}
@凌驾
public int getRowCount(){
返回linhas.size();
}
@凌驾
public int getColumnCount(){
返回colunas.length;
}
@凌驾
公共字符串getColumnName(int列){
返回colunas[列];
}
@凌驾
公共类getColumnClass(int列){
返回(getValueAt(0,列).getClass());
}
@凌驾
公共对象getValueAt(int行索引、int列索引){
clientforneedordo cf=linhas.get(rowIndex);
开关(列索引){
案例0:
返回cf.getNome();
案例1:
返回cf.getConta();
违约:
抛出新的IndexOutOfBoundsException(“Coluna incorreta”);
}
}
公共空白清除(JTable表格){
表.setRowSorter(空);
int indicateantigo=this.getRowCount();
linhas.clear();
int indiceNovo=this.getRowCount();
此。已删除FireTableRowsDetected(指示灯TIGO,指示灯NOVO);
}
公共布尔值为空(){
return linhas.isEmpty();
}
公共无效添加(客户需要添加到cf){
linhas.add(cf);
int index=this.getRowCount();
fireTableRowsInserted(索引,索引);
}
公共无效添加列表(列表、JTable表){
int tamanhoAntigo=this.getRowCount();
linhas.addAll(列表);
int tamanhoNovo=this.getRowCount()-1;
这是一个消防队(tamanhoAntigo,tamanhoNovo);
表.setAutoCreateRowSorter(真);
}
需要获得的公共客户(int i){
返回linhas.get(i);
}
}
下面的代码可以用数据填充my Jtable:

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();
}
private void realizarBusca(字符串名称){
IContaDAO=new ContaDAO();
布尔值isFornecedor=radioFornecedor.isSelected();
列表编号=
在前面的容器(isFornecedor,nome)上的容器容器容器;
TableModelClientForNeckedor.clear();
TableModelClientForNecedor.addList(Retrono,TableClientForNecedor);
TableClientForNeceDor.updateUI();
}
对我来说一切都很好,当我对我的Jtable进行可视化排序时也没问题,问题是当我在对Jtable进行排序后单击其中的某一行时,该行没有更新

有人能帮我吗?
我将不胜感激,因为我从昨天就开始使用它,但仍然无法找到解决它的方法。

请查看JTable中的方法
convertRowIndexToModel()
convertRowIndexToView()

对表进行排序时,视图中行的索引与模型中的索引不再匹配,您必须使用上述方法从索引转换到视图,反之亦然


例如,如果调用
JTable.getSelectedRow()
,将获得所选行的视图索引。您必须将其转换为模型索引(使用
convertRowIndexToModel()
),才能从模型中的列表中获取所选对象。

查看JTable中的方法
convertRowIndexToModel()
convertRowIndexToView()

对表进行排序时,视图中行的索引与模型中的索引不再匹配,您必须使用上述方法从索引转换到视图,反之亦然


例如,如果调用
JTable.getSelectedRow()
,将获得所选行的视图索引。您必须将其转换为模型索引(使用
convertRowIndexToModel()
),才能从模型中的列表中获取所选对象。

Hey JB,感谢您的回复。我尝试了这个方法,但索引仍然没有改变:int index=tabelaclientfornecedor.getSelectedRow();clientfornecedordto dto=(clientfornecedordto)tabelaclientfornecedor.getModel().getValueAt(tabelaclientfornecedor.convertRowIndexToView(index),0);您必须使用convertRowIndexToModel(),而不是convertRowIndexToView()。嘿,JB,谢谢您的回复。我尝试了这个方法,但索引仍然没有改变:int index=tabelaclientfornecedor.getSelectedRow();clientfornecedordto dto=(clientfornecedordto)tabelaclientfornecedor.getModel().getValueAt(tabelaclientfornecedor.convertRowIndexToView(index),0);必须使用convertRowIndexToModel(),而不是convertRowIndexToView()。不要使用updateUI()方法。没有任何理由这样做。不要使用updateUI()方法。没有任何理由这样做。