I want to add data from a table row to an array depending on their checkbox value in the boolean column of that row.
Here is the JTable component code:
String[] columnNames = {"Select", "Status", "Modified Files"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) return Boolean.class;
else return String.class;
}
};
...
model.addRow(new Object[]{Boolean.FALSE, changeType, filePath});
}
Then here is the attempt at adding data from said rows to an array:
if (modifiedFilesTable.isEditing()) modifiedFilesTable.getCellEditor().stopCellEditing();
for (int i = 0; i < model.getRowCount(); i++) {
Boolean isSelected = (Boolean) model.getValueAt(i, 0);
String filePath = (String) model.getValueAt(i, 2);
if (Boolean.TRUE.equals(isSelected)) {
filesToCommit.add(new File(filePath));
}
}
(I’ve tried to only include the relevant code)
What I’ve found is, what the checkbox is assigned on creation here :
model.addRow(new Object[]{Boolean.FALSE, changeType, filePath});
Determines the value of isSelected and the checkboxes value is disregarded. So if it was Boolean.TRUE all files would be added to filesToCommit..
I have asked this question before but loaded the question with too much code and got no responses except one saying to simplify the question. So I believe this is a simple I can get it.
I have looked everywhere for a solution for this but nothing works. So any ideas on how to fix this would be much appreciated.
jbum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.