This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Re-ordering JTable rows with DnD??  RSS feed

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I need to add some functionality to a JTable where I can click on a row, and drag it, in the same JTable, to a new position in the table, and have it reordered in the new position when the user lets go. I can either do a live UI update where the row actually moves with the cursor up and down as the user drags, or I can use a visual indicator like an arrow or a line showing where the drop will leave the re-ordered item.
Ok, so, how to I do this?
Thanks!!
Rob
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could really use some info on this if anyone can help.

Or at least provide me with some links/sources??

Thanks!
Rob
 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not say how far along you are with this.
Here are some sources I found useful when I started
to use awt.dnd.
JavaWorld had an intro article on the package a few
years ago. Check
http://www.javaworld.com/javaworld/jw-03-1999/jw-03-dragndrop.html
JDK 1.4 includes changes in the package, and the
Java Developer Connection has a Question of the Week
page devoted to that. Check
http://developer.java.sun.com/developer/qow/archive/1/
You will need to work with TableModel classes as you
go. The Tutorial covers that in How to Use Tables at
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
For a component that drags and drops onto itself,
you will need to code something like:


If you really want to customize the display during
the drag operation, look at the methods in the
DragGestureListener interface, especially dragOver().
You may be happy with the default, though.
When you drop the row into the new position, you
will have to update whatever values your system
uses to sort the rows, if any. Otherwise your system
will lose the knowledge of that new order when you
start it next time. I am guessing that the order does
matter or you would not try to allow reordering.
HTH,
Joe
 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also looking for a code example of a jtable that allows sorting of rows. Sorting of columns seems pretty well documented, are there any resources for sorting rows? Did you get anywhere with this?
Cheers,
Steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!