当我删除JTable的最后一行时超出范围异常

时间:2018-01-11 09:50:27

标签: java swing jtable indexoutofboundsexception

如果我们在表中有n行并删除其索引为n-1的行,则此代码为Ok,但是当我删除其索引为n的行(或删除行之后的任何行)时,我会捕获索引约束异常! 我该如何解决这个问题?

 Thread registerThread = new Thread(){
    public void run(){
        try{
            int i =JOptionPane.showConfirmDialog(view, "Are you sure to delete these IP and port?" );
            if(i == 0){
                int selectedRow = workerMachineTable().getSelectedRow();
                System.out.println("selected row" + selectedRow);
                unRegister(selectedRow);
                dataModel.removeRow(selectedRow);
            }else return;
        }catch (Exception e){
            JOptionPane.showMessageDialog(view, e.getMessage(), "Error", 2);
            e.printStackTrace();
        }
    }
};

1 个答案:

答案 0 :(得分:0)

从所选行中减去一个

  int selectedRow = view.getWorkerMachineTable().getSelectedRow() - 1;