• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving multiple rows at a time in a jtable while keeping there selection

 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friendly folks here at the Ranch.

So I'm trying to implement up and down arrows in my JTable. I want the up arrow (which is actually a button on my JToolBar, not the actual up arrow) to move the selected row(S) up one assuming that the first selected isn't already in the first slot. If for example, i have selected rows 2 and 4, and i press up, they move to rows one and three and stay highlighted. However, if I press it again, they stay rows one and three because the first row is already as far "up" as it can go.

I was able to get the indexes to move when i had multiple rows selected, but I was having a hard time keeping the selected rows correct. I'm including my code that moves the one row at a time, but want to eventually implement things as stated above.

 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all you need to ascertain if any of the rows are the first row before trying to move any rows up. If the selected rows index is guaranteed to be in ascending order then you can just check if the first selected index is row 1 and if it is don't try and move any rows. If it isn't row 1 move all your rows, clear the current selection and finally add the new row indices to the row selection model. All this can be down via JTable methods.
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you should be using the setSelectionInterval() method. I thought that this would clear existing selections and than reset the selection.

Instead I would try to clear all the selections. Then when you move the row I would use the addSelectionInterval(...) method.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I don't think you should be using the setSelectionInterval() method. I thought that this would clear existing selections and than reset the selection.

Instead I would try to clear all the selections. Then when you move the row I would use the addSelectionInterval(...) method.


That doesn't work. When you use the addSelectionInterval, it doesn't work if the rows you are moving are not grouped up. IS there a way to manually change a row selection without affecting other row selections? (not using the keyboard but pragmatically)
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the addSelectionInterval, it doesn't work if the rows you are moving are not grouped up

Works fine for me:


Without a SSCCE we can't be of much help.
 
K. Tsang
Bartender
Posts: 3526
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. First the JTable's default selection mode is ListSelectionModel.MULTIPLE_INTERVAL_SELECTION according JavaDoc.

Now using setSelectionInternal() will make the rows selected "inclusive" doesn't it? Again this is what you want I think.

One way I can think so is given you are using an array already, instead of using moveRow() and stuff ... is to change that row's background color using setSelectionBackground(Color) (this method sets color for cell instead of entire row).

This approach will practically make up and down actions the same.

Don't know if this will work but worth a try.

Another different approach is instead of highlighting the rows, why not use an extra column with checkboxes?
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Now using setSelectionInternal() will make the rows selected "inclusive" doesn't it? Again this is what you want I think.

What method is setSelectionInternal()? do you mean setRowSelectionInterval()? if so then yes this does allow you to select contiguous rows but that is not what the OP wants to do.

K. Tsang wrote: One way I can think so is given you are using an array already, instead of using moveRow() and stuff ... is to change that row's background color using setSelectionBackground(Color) (this method sets color for cell instead of entire row).
...

No, just do what Rob and I have said ie add the rows to the selection model. You can add multiple rows and they don't have to be contiguous as the code Rob has supplied shows.
 
K. Tsang
Bartender
Posts: 3526
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
What method is setSelectionInternal()? do you mean setRowSelectionInterval()? if so then yes this does allow you to select contiguous rows but that is not what the OP wants to do.


Yes my typo.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
K. Tsang wrote:Now using setSelectionInternal() will make the rows selected "inclusive" doesn't it? Again this is what you want I think.

What method is setSelectionInternal()? do you mean setRowSelectionInterval()? if so then yes this does allow you to select contiguous rows but that is not what the OP wants to do.

K. Tsang wrote: One way I can think so is given you are using an array already, instead of using moveRow() and stuff ... is to change that row's background color using setSelectionBackground(Color) (this method sets color for cell instead of entire row).
...

No, just do what Rob and I have said ie add the rows to the selection model. You can add multiple rows and they don't have to be contiguous as the code Rob has supplied shows.


You guys were right, i had just screwed something up. it works beautifully
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic