Why is my model.addRow function not working?

678 Views Asked by At

The goal of the code is to add an object to my jTable. I have two classes called Task and TaskUI.

My initial expectation was to see a MessageDialog that says "Valid Task Details" and my object being added to the jTable. However, the only successful event was the program displaying the MessageDialog.

I have not received any syntax, logical or runtime error messages.

I have initially attempted to follow a step-by-step approach at creating a to-do list through this video: ToDo List in JAVA Netbeans but I feel that there is not enough separation between the UI and working code. What I mean by this is that a backend cannot be 'plugged into' a different UI that uses all the methods appropriately.

Please note that I have recently edited the code, however, I need help to add the addTask() method

Task

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class Task {
private String name;
private String priority;
private String type;
private String time;
private boolean complete;

public Task(String name, String priority, String type, String time, boolean complete) {
    this.name = name;
    this.priority = priority;
    this.type = type;
    this.time = time;
    this.complete = complete;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPriority() {
    return priority;
}

public void setPriority(String priority) {
    this.priority = priority;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public boolean isComplete() {
    return complete;
}

public void setComplete(boolean complete) {
    this.complete = complete;
}

public class TaskTableModel extends AbstractTableModel {
    
    private String[] columnNames = {"Task","Priority","Type","Estimated Time","Status"};

    private List<Task> tasks;
    
    public TaskTableModel() {
        tasks = new ArrayList<Task>();
    }
    
    public TaskTableModel(List<Task> tasks) {
        this.tasks = tasks;
    }
    
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
    
    @Override
    public int getRowCount() {
        return tasks.size();
    }        

    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case 4: return boolean.class;
            default: return String.class;
        }
    }
    
    @Override
    public boolean isCellEditable(int row, int column) {
        switch (column) {
            case 4: return true;
            default: return false;
        }
    }
    
    @Override
    public Object getValueAt(int row, int column) {
        Task task = getTask(row);
        switch (column) {
            case 0: return task.getName();
            case 1: return task.getPriority();
            case 2: return task.getType();
            case 3: return task.getTime();
            case 4: return task.isComplete();
            default: return null;
        }
    }
    
    @Override
    public void setValueAt(Object value, int row, int column) {
        Task task = getTask(row);
        switch (column) {
            case 0: task.setName((String)value);  
            break;
            case 1: task.setPriority((String)value);
            break;
            case 2: task.setType((String)value);
            break;
            case 3: task.setTime((String)value);
            break;
            case 4: task.setComplete((boolean)value);
            break;
        }
        fireTableCellUpdated(row, column);
    }
    
    public Task getTask(int row) {
        return tasks.get(row);
    }
    
    public void addTask(Task task) {
        insertTask(getRowCount(), task);
    }
    
    public void insertTask(int row, Task task) {
        tasks.add(row, task);
        fireTableRowsInserted(row, row);
    }
    
    public void removeTask(int row) {
        tasks.remove(row);
        fireTableRowsDeleted(row, row);
    }
}//end TaskTableModel

@Override
public String toString() {
    return "Task{" + "name=" + name + ", priority=" + priority + ", type=" + type + ", time=" + time + ", complete=" + complete + '}';
}
}//end Task

TaskUI

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

public class TaskUI extends javax.swing.JFrame {
   boolean validTask;
   Task obj;
   DefaultTableModel model = new DefaultTableModel();

   /**
   * Creates new form TaskUI
   */
   public TaskUI() {
       initComponents();
   }

  private void btnMakeObjectActionPerformed(java.awt.event.ActionEvent evt) {                                              
    String name = txfTask.getText();
    String priority = (String) cmbPriority.getSelectedItem();
    String type = (String) cmbType.getSelectedItem();
    String time = txfTime.getText();
    boolean complete = false;
    if(txfTask.getText().length() > 0 && cmbPriority.getSelectedItem() != "--" && cmbType.getSelectedItem() != "--" && txfTime.getText().length() > 0) {
        JOptionPane.showMessageDialog(null, "Valid Task Details");
        validTask = true;
        obj = new Task (name,priority,type,time,complete);
        /**The following error message appears when adding the addTask(...) method:
         * cannot find symbol
         * symbol:   method addTask(Object[])
         * location: variable model of type DefaultTableModel
         */
        model.addTask(new Object[]{tblTaskInfo.getRowCount()+1, obj});
    }
    else {
        JOptionPane.showMessageDialog(null, "Invalid Task Details");
    }   
}

}

Here is a picture of my UI enter image description here

0

There are 0 best solutions below