How do I get a line back on my jTable?

Asked

Viewed 196 times

1

I’d like to know how to retrieve information from a line of mine JTable, follows the code line of the same.

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();
    }
}

1 answer

3


Just use getValueAt:

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

The code does not make much sense the way it was presented, because this variable tabela is starting a DefaultTableModel empty, depending on the moment you call this method, it can return empty. But if you guarantee that this variable will match 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 Indice 0, therefore, a table with 15 rows will have indices from 0 to 14.

To scan a selected row and grab all columns, one can do so:

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);)
}
  • I tried to apply the code but got an error: "Exception in thread "AWT-Eventqueue-0" java.lang.Arrayindexoutofboundsexception: -1 ", I created an operation: "public void Recovers() { int selectedRowIndex = jTable1.getSelectedRow(); int columnCount = jTable1.getColumnCount(); for (int i = 0; i < columnCount; i++) { System.out.println(table.getValueAt(selectedRowIndex, i)); } } " and applied on class initialization, you know where I went wrong?

  • @netoschneider add a [mcve] editing the question, without it there is no way to test and see what could cause the error.

  • I already solved my big one, I forgot I was using a for multiply the actions of the method Executor to add lines to jTable, now it is 100% working, you know tell me how I make the mouse click correspond to certain table item? Thank you very much!!!

  • @netoschneider if the problem helped you, you can mark how accept the answer by clicking on the v on her left side, in this way, she will serve as a reference for doubts similar to her :)

  • @netoschneider getseletedrow already picks the selected line, did not understand his other doubt.

  • I would like to know how I do to the program identify the mouse click to so pass the selection the line capture action.

  • I already solved it, thank you very much!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.