0

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.

yams
  • 942
  • 6
  • 27
  • 60
  • 1
    Isn't this correct behaviour? If nothing is selected it returns -1? How can anything be selected if there's nothing in the table? – ldam Jan 08 '13 at 20:35
  • Sorry I had to correct this. But After removing the rows when I select a new row after rows where re added it returns a -1. – yams Jan 08 '13 at 21:14
  • Logan re read my post above. I remove all the rows, reload some rows and make a selection and the getSelectedRow returns a -1. – yams Jan 08 '13 at 21:20
  • 1
    Can you provide a [SSCCE](http://sscce.org/) the demonstates the problem. It's impossible to "guess" at how you are working and why this might be occurring to you. – MadProgrammer Jan 08 '13 at 22:46
  • I have made edits to the above code, MadProgrammer. – yams Jan 08 '13 at 22:55

2 Answers2

2
  • 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
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Sorry I had to edit my post but when I remove rows then re add rows and select a row I am returning a -1. – yams Jan 08 '13 at 21:16
  • removed row by default lost selection too, for why reason you helt selection ? – mKorbel Jan 08 '13 at 21:55
  • I am removing rows as part of the user doing a File menu then "New" so I need to clear the tables then readd items to the table. – yams Jan 08 '13 at 22:08
0

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.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148