aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Moving multiple rows at a time in a jtable while keeping there selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Moving multiple rows at a time in a jtable while keeping there selection" Watch "Moving multiple rows at a time in a jtable while keeping there selection" New topic
Author

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

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
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

Joined: Aug 07, 2007
Posts: 2248
    
  47
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

Joined: Jun 13, 2009
Posts: 2177
    
    7
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

Joined: Oct 05, 2007
Posts: 69
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

Joined: Jun 13, 2009
Posts: 2177
    
    7
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

Joined: Sep 13, 2007
Posts: 2379
    
    7

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?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
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

Joined: Sep 13, 2007
Posts: 2379
    
    7

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

Joined: Oct 05, 2007
Posts: 69
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Moving multiple rows at a time in a jtable while keeping there selection