I’m working on a java gui that helps commit files to a SVN Repository. Modified files appear in a JTable that consists of three columns: boolean checkbox, a string status of the modification and a string file path of the modified file.
I want the boolean checkbox to indicate what files to commit at the same time under the same commit message however can’t get the method to recognise if the boolean values have been changed or not, and as of now it only recognises the value of the boolean checkbox as what is on creation.
I’m using java swing for the gui design.
Here is the relevant code from the CommitView class.
//Commit method performs on button click.
commitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
commitInstance.doCommit();
}
});
}
private void createUIComponents() {
/***** CUSTOM CODE FOR JTABLE *****/
//Fetch modified files list.
List<String[]> modifiedFilesList = ModifiedFilesDisplay.getModifiedFiles();
modifiedFiles = new String[modifiedFilesList.size()];
//Grabs file path to the files that have been modified.
for(int i = 0; i < modifiedFilesList.size(); i++) {
modifiedFiles[i] = modifiedFilesList.get(i)[0];
}
//Assign column names and class.
String[] columnNames = {" ", "Status", "Modified Files"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0){
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return Boolean.class;
case 1,2: return String.class;
default: return Object.class;
}
}
};
//Assign a letter to display depending on the type of modification.
for (String[] fileInfo : modifiedFilesList) {
String filePath = fileInfo[0];
String status = fileInfo[1];
char changeType;
switch (status) {
case "modified":
changeType = 'M';
break;
case "added":
changeType = 'A';
break;
case "deleted":
changeType = 'D';
break;
case "unversioned":
changeType = 'U';
break;
default:
changeType = '?';
break;
}
// IF SET TO BOOLEAN.TRUE, METHOD READS ALL FILES AS SELECTED EVEN IF UNSELECTED IN GUI TABLE, AND WILL COMMIT ALL FILES.
//IF FALSE, "NO FILES SELECTED" ERROR IS THROWN EVEN IF CHECKBOXES ARE SELECTED.
model.addRow(new Object[]{Boolean.FALSE, changeType, filePath});
}
//Initialise JTable.
modifiedFilesTable = new JTable(model);
//Boolean checkbox cell editor and renderer.
modifiedFilesTable.getColumnModel().getColumn(0).setCellEditor(modifiedFilesTable.getDefaultEditor(Boolean.class));
modifiedFilesTable.getColumnModel().getColumn(0).setCellRenderer(modifiedFilesTable.getDefaultRenderer(Boolean.class));
//Set column widths.
modifiedFilesTable.getColumnModel().getColumn(0).setMaxWidth(20);
modifiedFilesTable.getColumnModel().getColumn(1).setMaxWidth(50);
And here is the doCommit method.
public void doCommit(){
//Initialise.
DefaultTableModel model = (DefaultTableModel) modifiedFilesTable.getModel();
List<File> filesToCommit = new ArrayList<>();
//Tells the editor to stop editing and accept any partially edited value as the value of the editor.
if (modifiedFilesTable.isEditing()) modifiedFilesTable.getCellEditor().stopCellEditing();
//Iterate through modified files and grab those with Boolean.TRUE to add to filesToCommit.
for (int i = 0; i < model.getRowCount(); i++) {
Boolean isSelected = (Boolean) model.getValueAt(i, 0);
String filePath = (String) model.getValueAt(i, 2);
//debug
System.out.println("Row " + i + " : isSelected: " + isSelected + ", filePath = " + filePath);
if (Boolean.TRUE.equals(isSelected)) {
filesToCommit.add(new File(filePath));
}
}
//Assign text to commitMessage.
String commitMessage = commitMessageField.getText().trim();
//Error handling if fields are empty.
if (filesToCommit.isEmpty()) {
JOptionPane.showMessageDialog(null, "No files selected.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (commitMessage.isEmpty()){
JOptionPane.showMessageDialog(null, "Commit message cannot be empty.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
//Commit Method
SVNCommitClient commitClient = clientManager.getCommitClient();
SVNCommitInfo commitInfo = commitClient.doCommit(
filesToCommit.toArray(new File[0]),
false,
commitMessage,
null,
null,
false,
false,
SVNDepth.INFINITY
);
//Error handling for failed commits.
if (commitInfo.getErrorMessage() != null) {
JOptionPane.showMessageDialog(null, "Commit Failed: " + commitInfo.getErrorMessage().getFullMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Commit successful. Revision : " + commitInfo.getNewRevision(), "Success", JOptionPane.INFORMATION_MESSAGE);
}
} catch (SVNException svne) {
JOptionPane.showMessageDialog(null, "Error during commit: " + svne.getErrorMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
The line of code:
model.addRow(new Object[]{Boolean.FALSE, changeType, filePath});
sets isSelected for all files as whatever the boolean value is and does not change according to the checkbox value.
I have tried altering this but have had no success.
Also, not seen in this code but have tried stopCellEditing
but made no difference.
Any help 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.