Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Default Table Model's removeRow method problem

843805Dec 18 2006 — edited Dec 21 2006
I'm using the following code for the listener and although when I check the value of the selected Row it shows the correct index, when I call the tblmodel.removeRow() method it throws an exception -1 ....
    public void valueChanged(ListSelectionEvent e){
        if(e.getSource() == tbl.getSelectionModel() && tbl.getRowSelectionAllowed()){

            // Get the data model for this table
            //TableModel model = (TableModel)tbl.getModel();

            // Determine the selected model
            int selectedRowIndex = tbl.getSelectedRow(); 
            String id = (String)tblmodel.getValueAt(tbl.getSelectedRow(),0);
            String value = (String)tblmodel.getValueAt(tbl.getSelectedRow(),tbl.getSelectedColumn());

            // Display the selected item
            if (! e.getValueIsAdjusting()){
                if(value.equals("Delete")){
                   if(JOptionPane.showConfirmDialog(null,"Are you sure you want to delete account no "+id + "\n Row Index: " + selectedRowIndex + "\nModel Row Count : "+tblmodel.getRowCount(), "Confirmation",JOptionPane.OK_CANCEL_OPTION)==0){
                       try {
                           deleteAccount(id);
                           tblmodel.removeRow(selectedRowIndex);
                       }
                       catch (ArrayIndexOutOfBoundsException ex) {
                           JOptionPane.showMessageDialog(null, ex.getMessage() + "Returned Row Index is : " + selectedRowIndex, "Error", JOptionPane.ERROR_MESSAGE);
                           System.exit(1);
                       }                       
                   }
                }
                //JOptionPane.showMessageDialog(null,"The Value recieved is : "+id + "-"+value, "Value Recieved", JOptionPane.PLAIN_MESSAGE);
            }
        }       
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 18 2007
Added on Dec 18 2006
9 comments
400 views