How can my code recognise the value of a Boolean checkbox in a JTable?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
//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);
</code>
<code> //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); </code>

        //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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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);
}
}
</code>
<code> 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); } } </code>
    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.

New contributor

jbum is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật