• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

How do I move the selected rows to the top in JTable?

Hi,
How do I move the selected table rows to the top of the table in JTable?
i.e.
If I have selected rows 1,4,5, if I click "move selected" button, the rows move to the top and stay selected.

Thanks
Jamie
0
jamie_lynn
Asked:
jamie_lynn
  • 3
  • 3
  • 2
2 Solutions
 
objectsCommented:
table. setRowSelectionInterval(0, 0);

0
 
__geof__Commented:
Depends on your TableModel. How is it done? How are the data added to the JTable?
0
 
jamie_lynnAuthor Commented:
objects,
doesn't setRowSelectionInterval selects the rows?
I want to move my selected rows to the top of the tables

geof,
I use a TableModel to add the data.. was this what u were asking?

Thanks
Jamie
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
sorry misread your question :)

As geof states, you would rearrange your data in your table
The DefaultTableModel class has a moveRow() method you could  use

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html#moveRow(int,%20int,%20int)

0
 
__geof__Commented:
Yes and No. Still need to know how the data are added to the TableModel. Is it an ArrayList or an Array or something else?
0
 
jamie_lynnAuthor Commented:
objects,
Does moveRow() keep the selection when the row is moved?

geof,
It's something else.. but if you show me with ArrayList, I can make it work with mine.
0
 
objectsCommented:
good question, not sure. I would guess the selection stays (but does not move with the rows). May be you need to adjust the selection  as part of the move.

0
 
__geof__Commented:
Quite a lot of code to make it work with ArrayList since you need to transform it to Array but moveRow() should work alright. I don't think it will keep the selection but you can mark the needed line as selected with the code below where index0 and index1 would be 0 to select the first lien.
table.getSelectionModel().setSelectionInterval(int index0, int index1)

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now