yams
yams

Reputation: 952

Removing all the Rows then reloading rows in a JTable is causing the getSelectedRow to return a -1

When using some software I have created that has a gui with a JTable with the DefualtTableModel called validAcTableModel, When I initilize the validAcTable this is the logic I am using:

    ListSelectionModel cellSelectModel = validAcTable.getSelectionModel();
    cellSelectModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    cellSelectModel.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {

            if (d == 0) {
                    suggestedAcTable.clearSelection();
                d = 1;
            } else {

                String selectedAcData = null;
                String selectedSentData = null;
                String selectedDefData = null;
                String selectedBoolean = null;
                validAcTable.revalidate();
                int[] selectedRow = validAcTable.getSelectedRows();
                for (int i = 0; i < selectedRow.length; i++) {
                    selectedAcData = validAcTable.getValueAt(selectedRow[i], 0).toString();
                    selectedDefData = validAcTable.getValueAt(selectedRow[i], 1).toString();
                    selectedBoolean = validAcTable.getValueAt(selectedRow[i], 2).toString();
                    selectedSentData = getSentence((String) validAcTable.getValueAt(selectedRow[i], 0));
                    if (selectedSentData == null) {
                        selectedSentData = "";
                    }
                }
                Acronym acr = new Acronym(selectedAcData, selectedSentData, selectedDefData, false);
                changedAcList.add(acr);
                //String has a white space....need to redo this...
                currentAccTextField.setText(selectedAcData);
                currentSentenceTextArea.setText(selectedSentData);
                currentDefTextArea.setText(selectedDefData);
                if (selectedBoolean != null) {
                    if (selectedBoolean.equals("true")) {
                        acceptAccButton.setEnabled(false);
                        validLabel.setText("Definition is valid in document");
                    } else {
                        acceptAccButton.setEnabled(true);
                        validLabel.setText("Definition is not valid");
                    }
                }

                d = 0;
            }
        }
    });

When I click the New Button on my GUI and use

  validAcTableModel.getDataVector().removeAllElements();

When I try to reload the table and select an item and get the selectedRow using:

  private void acceptAccButtonActionPerformed(java.awt.event.ActionEvent evt) { 
       if (validAcTable.getSelectedRow() >= 0) {
             StringBuilder acDocText = new StringBuilder();
             String acNameDefthmlText = "";
       }
  }

This always returns a negative one on the selected Row after removing all of the elements then re add rows when I select a row. I would appreciate some help. I am using a ListSelectionListener for the valueChanged.

Upvotes: 0

Views: 488

Answers (2)

Paul Vargas
Paul Vargas

Reputation: 42040

For clear the data, change to method javax.swing.table.DefaultTableModel.setRowCount(int);

((DefaultTableModel) validAcTable.getModel()).setRowCount(0);

For re-populate the model, use:

javax.swing.table.DefaultTableModel.setDataVector(Vector, Vector);
javax.swing.table.DefaultTableModel.setDataVector(Object[][], Object[])

Remember to use the same instance of the model.

Use the helper methods in DefaultTableModel. These launch the appropriate event for update the UI.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109823

  • When I try to reload the table and select an item and get the selectedRow using

set selection to desired row programatically JTable.setRowSelectionInterval(int index0, int index1);

  • this always returns a negative one on the selected Row after removing all of the elements.

Integer -1 returns only if any row isn't selected from API

public int getSelectedRow()

Returns the index of the first selected row, -1 if no row is selected.

Returns:
    the index of the first selected row
  • for better help sooner post an SSCCE

Upvotes: 2

Related Questions