Pessoal,
Não estou conseguindo fazer com que ao mostrar uma JTable a tela (JScrollPane) seja rolado automaticamente para a linha selecionada.
A linha selecionada não está visível na tela, tentei implementar o scrollRectToVisible mas não funcionou, não dá erro mas também não faz o necessário.
O meu método onde implementei este tratamento é este:
public void SelecionaLinha(Object pCodigo) {
int iLinha = -1;
for (int iSeq = 1 ; iSeq < this.getRowCount() ; iSeq++) {
if (this.getValue(iSeq,0).equals(pCodigo)) {
iLinha = iSeq;
break;
}
}
if (iLinha != -1) {
this.changeSelection(iLinha,0,false,false);
} else {
this.changeSelection(0,0,false,false);
}
if ( this.getParent() instanceof JViewport ){
JViewport viewport = (JViewport)this.getParent();
Rectangle rect = this.getCellRect(iLinha, 0, true);
Point pt = viewport.getViewPosition();
rect.setLocation(rect.x-pt.x, (rect.y-pt.y)+this.getRowHeight());
viewport.scrollRectToVisible(rect);
}
}
Da linha 18 até a linha 24 é a lógica para rolar a tela, mas não está "rolando"…
Na definição do JScrollPane estou fazendo assim:
JScrollPane spTabela = new JScrollPane();
spTabela.setViewportView(tabela);
tabela.SelecionaLinha(pCodSelected);
Alguém sabe o dizer porque não funciona?