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.