Check if you have reached the last row of the table

Asked

Viewed 431 times

0

I’m using this code to do a approved or flunked, but when he gets to the last line, he’s not checking if he’s on the last line and he doesn’t give me the result.

I would like to know how to check if you have reached the last row of the table.

   int coluna = CadresultadoTabela.getSelectedColumn();

    if (evt.getKeyCode() == KeyEvent.VK_TAB) {
        int linha = CadresultadoTabela.getSelectedRow();
        Object obj = modelo.getValueAt(linha, 30);
        Object obj2 = modelo.getValueAt(linha, 31);
        Object obj3 = modelo.getValueAt(linha, 32);

        if ((obj == null || obj.toString().equals("")) && (obj2 == null || obj2.toString().equals(""))) {
            TabPreenchimento();
        } else {
            VerificarResultdo();
            if (modelo.getValueAt(linha, 33).equals("Reprovado")) {
                if (obj3 == null || modelo.getValueAt(linha, 32).equals("")) {
                    CadresultadoTabela.changeSelection(linha, 32, false, false);
                } else {
                    TabPreenchimento();
                }
            } else {
                TabPreenchimento();
            }
        }
    } else if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        int linha = CadresultadoTabela.getSelectedRow() - 1;
        Object obj = modelo.getValueAt(linha, 30);
        Object obj2 = modelo.getValueAt(linha, 31);
        Object obj3 = modelo.getValueAt(linha, 32);

        if ((obj == null || obj.toString().equals("")) && (obj2 == null || obj2.toString().equals(""))) {
            //VERIFICA SE A SELECAO ESTA NA ULTIMA LINHA
            if (CadresultadoTabela.getSelectedRow() != CadresultadoTabela.getRowCount()) {
                EnterPreenchimento();
            } else {
                TabPreenchimento();
            }
        } else {
            //VERIFICA SE A SELECAO ESTA NA ULTIMA LINHA
            if (CadresultadoTabela.getSelectedRow() != CadresultadoTabela.getRowCount()) {
                VerificarResultdoEnter();
            } else {
                VerificarResultdo();
            }
            if (modelo.getValueAt(linha, 33).equals("Reprovado")) {
                if (obj3 == null || modelo.getValueAt(linha, 32).equals("")) {
                    CadresultadoTabela.changeSelection(linha, 32, false, false);
                } else {
                    //VERIFICA SE A SELECAO ESTA NA ULTIMA LINHA
                    if (CadresultadoTabela.getSelectedRow() != CadresultadoTabela.getRowCount()) {
                        EnterPreenchimento();
                    } else {
                        TabPreenchimento();
                    }
                }
            } else {
                //VERIFICA SE A SELECAO ESTA NA ULTIMA LINHA
                if (CadresultadoTabela.getSelectedRow() != CadresultadoTabela.getRowCount()) {
                    EnterPreenchimento();
                } else {
                    TabPreenchimento();
                }
            }
        }
    }

1 answer

1

To check if the last row is selected, you need to compare the index of the selected row with table.getRowCount() -1, since the indexes start at 0.

if(table.getSelectedRow() == table.getRowCount() -1){

    //ultima linha selecionada

} 

I believe in your code, the correct way to check this something like this:

if (CadresultadoTabela.getSelectedRow() == CadresultadoTabela.getRowCount() - 1)

Remember that if you have using some type of filter in the table(RowSorter), accurate convert the selection index to the equivalent in the model.

  • In this case did not work would not pq am declaring int line = Cadresultadoptebela.getSelectedRow() - 1;???

  • @Rafaaw have no idea, I would need to test your code for a more specific answer. In my test example this way worked correctly.

  • Strange, I’ll give an analysis and anything I contact.

Browser other questions tagged

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