Removing multiple selections from a jtable

Right now I have a method that gets the selected row.

//////////////////////////////////////////////////////////////////////////
   //                           removeTableRow()                           //
   //////////////////////////////////////////////////////////////////////////
   public void removeTableRow() {
      int row = selectTable.getSelectedRow();
      if (row >= 0) {
         myModel.removeSelectedRow(row);
      }
      else {
         JOptionPane.showMessageDialog(null, "You need to select a dataset to remove.");
      }
   }

Open in new window


Then I have a method that removes the selection from the table

//////////////////////////////////////////////////////////////////////////
   //                       removeSelectedRow()                            //
   //////////////////////////////////////////////////////////////////////////
   public void removeSelectedRow(int row) {
      boolean b = (rds.remove(row) != null);
      fireTableRowsDeleted(row, row);
      getOKButton();
   }// end removeSelectedRow()

Open in new window


rds is my arraylist the table is built from.

The user needs to be able to make multiple deletes.  
If my table looks like this

row1
row2
row3
row4
row5

right now the user can select any of the rows click a button and the selection is removed from the table
but if the user needs to remove row2 and row 4, I do not know how to handle that.

I need of guidance
jkteaterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
Look at this solution for multiple row selection in JTable:

http://stackoverflow.com/questions/2141273/selecting-multiple-rows-of-jtable
0
jkteaterAuthor Commented:
so I would need to do the selection listener as well?
0
for_yanCommented:
As always, listener or not depends on the mode you want to operate. If you want to react to the act of selection you'll need to have selection listener; if you say have a button which will initiate the action, then you just may check if the rows/cells are selected when you process the event coming from button
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

CEHJCommented:
>>int row = selectTable.getSelectedRow();

Use instead

http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#getSelectedRows()
0
jkteaterAuthor Commented:
here is what I have

//////////////////////////////////////////////////////////////////////////
   //                           removeTableRow()                           //
   //////////////////////////////////////////////////////////////////////////
   public void removeTableRow() {
      int[] row = selectTable.getSelectedRows();
      if (row.length > 0) {
         for(int i = 0; i < row.length; i++){
            myModel.removeSelectedRow(row[i]);
         }
      }
      else {
         JOptionPane.showMessageDialog(null, "You need to select a dataset to remove.");
      }
   }

Open in new window


I know it is selecting correctly, but I think when I am passing the values to  
myModel.removeSelectedRow(row[i]);

Open in new window

 something is not right.  It will delete one of the selected rows and through a error IndexOutOfBounds on this line

boolean b = (rds.remove(row) != null);

Open in new window


in this method

 
public void removeSelectedRow(int row) {
      boolean b = (rds.remove(row) != null);
      fireTableRowsDeleted(row, row);
      getOKButton();
   }// end removeSelectedRow()

Open in new window

0
for_yanCommented:
I think when you are removing multiple rows, perhaps you should
go in the order from rows with higher numbers to rows with lower numbers
- first you remove row number 5 and then row number 3, not the other way around
0
for_yanCommented:
Perhaps like this:

//////////////////////////////////////////////////////////////////////////
   //                           removeTableRow()                           //
   //////////////////////////////////////////////////////////////////////////
   public void removeTableRow() {
      int[] row = selectTable.getSelectedRows();
Arrays.sort(row);
      if (row.length > 0) {
         for(int i = row.length-1; i >= 0; i--){
            myModel.removeSelectedRow(row[i]);
         }
      }
      else {
         JOptionPane.showMessageDialog(null, "You need to select a dataset to remove.");
      }
   }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkteaterAuthor Commented:
so I should reverse the array before reading it in the method?
0
jkteaterAuthor Commented:
oops - I will try your suggestion
0
jkteaterAuthor Commented:
Yes that works - Thanks
0
for_yanCommented:
Great!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.