0

Im building a program that lets you enter the course name, course time and course day and sets the course name in a one week setting and this is the code i tried to use to remove a course from the jtable by simply inputing the name of the course and having algorithm go through the table until it finds the cell with the same course Name.

  JButton btnRemoveCourse = new JButton("Remove");
    btnRemoveCourse.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) {
            String courseName = JOptionPane.showInputDialog("Enter name of course you want to remove");
            int i = 0,j = 0;
            while(table.getValueAt(i, j) != courseName)
            {
                i++;
                j++;
            }
            table.setValueAt("", i, j);
        }
    });

I basically want the user to enter the course name that they want to remove and then search for that course name in the jtable and set that cell to empty.

Shervin
  • 1
  • 1
  • Yep, sure, but why not remove the associated row? – MadProgrammer Apr 26 '19 at 04:03
  • `table.getValueAt(i, j) != courseName` >> that is not the proper way to verify whether objects are equal. Best try `!courseName.equals(table.getValueAt(i, j))`. – TT. Apr 26 '19 at 04:41
  • As @MadProgrammer wrote. Wouldn't it be easier for the user to simply select the row in the `JTable` [s]he wants removed and then click on your "Remove" button? Then in your `actionPerformed()` method, you simply remove the selected row. – Abra Apr 26 '19 at 05:05
  • @TT. thanks worked perfectly. – Shervin Apr 26 '19 at 13:43

0 Answers0