Xử lý xóa 1 row trong JTable

Mình đang học Gui đang thừ viết 1 ctqly mà mình làm tớ phần này thì bị lỗi cứ throw exception hoài ma ko bik sửa thế nào bác nào cho chút ý kiên để mình viết dc ko!
đây là đoạn code mình viết

 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {       
@Override       
public void valueChanged(ListSelectionEvent e) {
            int row = table.getSelectedRow();
            txtid.setText(model.getValueAt(row, 0).toString()); //line 222 here
            txtname.setText(model.getValueAt(row, 1).toString());
            txtage.setText(model.getValueAt(row, 2).toString());
            txtmail.setText(model.getValueAt(row,3).toString());            }
   });

đoạn này viết nut xóa

    if (e.getSource().equals(btndel)) { 

            Student student= ds.searchstu(Long.parseLong(txtid.getText()));
            if(student==null)
            {
                 System.out.println("notfound ");
             }
        else{
                ds.delstu(Long.parseLong(txtid.getText())); 
                 System.out.println("found and del");
                    ds.printliststu();

                /**code del row here*/
DefaultTableModel models = (DefaultTableModel)table.getModel(); 
              int rows = models.getRowCount(); 
              for(int i = rows - 1; i >=0; i--)
                {
               model.removeRow(i); 
                  }

    }
    }

đây là phần compile của minh nó chạy tốt phần xóa nhung cứ chạy toi phần xóa row là bi lổi mình thử viết nhìu cách khác mà ko dc.

++ file++[1122, asda, 1234, [email protected]]
++ file++[3232, erdggggggggggg, 2222, [email protected]]
++ file++[2121, asdasd, 2121, asd@asdff]
found student
stu need del = Student [id=1122, name=asda, [email protected], age=1234]
begin del
++list++ Student[id=3232, name=erdggggggggggg, [email protected], age=2222]
++list++ Student[id=2121, name=asdasd, mail=asd@asdff, age=2121]

Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.Vector.elementData(Unknown Source) at
java.util.Vector.elementAt(Unknown Source) at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source) at .panel$2.valueChanged(Giaodien.java:222)
at .panel.delrows(panel.java:435)
at .panel.actionPerformed(panel.java:388)

JTable.getSelectedRow():

Returns the index of the first selected row, -1 if no row is selected.

Nếu không có dòng nào được chọn thì sẽ trả về -1.

Sai ở phương thức valueChanged() chứ không phải ở delrows().

Bạn phải kiểm tra trường row trước khi thực hiện các thao tác tiếp theo.

2 Likes

valuechange giờ sửa sao giờ bạn mính thử xóa hết mấy dòng đó đi xong nó báo exception String " "

Cuối cùng mình cũng sửa dc vấn đề là viết đoạn code này ko phù hợp

   table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {       
   @Override       
   public void valueChanged(ListSelectionEvent e) {
               int row = table.getSelectedRow();
               txtid.setText(model.getValueAt(row, 0).toString()); //line 222 here
               txtname.setText(model.getValueAt(row, 1).toString());
               txtage.setText(model.getValueAt(row, 2).toString());
               txtmail.setText(model.getValueAt(row,3).toString());            }
      });

cần phải dùng   public void mouseClicked(MouseEvent e)
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?