How do I retrieve a line in my jTable?

1

I would like to know how to retrieve information from a line of my JTable , follow the line of code of it.

Usuarios operacao = new Usuarios();
DefaultTableModel tabela = new DefaultTableModel();

private void adicionarLinhas(String nome, String patente, String sessao, String email) {
    this.tabela.addRow(new Object[]{nome, patente, sessao, email});
}

public void Executar() throws IOException, ClassNotFoundException {
    ArrayList<UsuariosBase> usuarios2 = operacao.LeituraDeRegistros();
    for (UsuariosBase registros : usuarios2) {
        tabela = (DefaultTableModel) jTable1.getModel();
        this.adicionarLinhas(registros.nome, registros.patente, registros.sessao, registros.email);
    }
}

public void Listar() throws IOException, ClassNotFoundException {
    operacao.RecuperadorDeNomes();
    for (int i = 1; i < operacao.totalDeArquivos; i++) {
        Executar();
    }
}
    
asked by anonymous 08.12.2016 / 00:08

1 answer

3

Just use getValueAt :

suaJTable.getModel().getValueAt(indiceLinha, indiceColuna);

The code does not make much sense as it was presented, because this variable tabela is starting an empty DefaultTableModel , depending on when you call that method, it can return empty. But if you guarantee that this variable will correspond to the model already applied in your JTable, just call the method by it:

tabela.getValueAt(indiceLinha, indiceColuna);

Remembering that columns and rows always start at index 0, so a table with 15 rows will have indexes from 0 to 14.

To scan a selected line and grab all the columns, you can do this:

int selectedRowIndex = jTable1.getSelectedRow();
int columnCount = suaJTable.getColumnCount();

for(int i = 0; i < columnCount; i++){
    //aqui você faz alguma coisa com o dado de cada coluna
    System.out.println(tabela.getValueAt(selectedRowIndex, i);)
}
    
08.12.2016 / 00:13