samedi 20 mai 2023

How to move the specific table row's data to another table once the checkbox on the same row is checked?

I have a table and for the first column, I have a checkbox for each row. How can I make it so that once the checkbox is checked, all of the data that belongs to the same row as the checkbox is moved to another table(which is on another frame)?

This is my first time here so I don't know if I'm doing this right but I'll be super grateful if anyone answers. Thank you.

I tried using an ActionListener for the checkbox but it didn't work or maybe I just did not do it right. This is my first time trying to create a system as a self learner so please bear with me.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
    if (txtTask.getText().equals("") || txtSubject.getText().equals("") || txtDeadline.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Enter all necessary data.");
    } else {
        String task = txtTask.getText();
        String subject = txtSubject.getText();
        String deadline = txtDeadline.getText();

        DefaultTableModel model = (DefaultTableModel) tableTasks.getModel();
        model.addRow(new Object[] { false, task, subject, deadline, false });

        JOptionPane.showMessageDialog(this, "New task has been added to the list . . . !");
        txtTask.setText("");
        txtSubject.setText("");
        txtDeadline.setText("");

        int lastRowIndex = model.getRowCount() - 1;
        Boolean isChecked = (Boolean) model.getValueAt(lastRowIndex, 0);
        if (isChecked) {
            Object[] rowData = new Object[tasksModel.getColumnCount()];
            for (int col = 0; col < tasksModel.getColumnCount(); col++) {
                rowData[col] = tasksModel.getValueAt(lastRowIndex, col);
            }
            finishedTasksModel.addRow(rowData);
            tasksModel.removeRow(lastRowIndex);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire