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?
 
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
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.