Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

How to reorder JTable

Thread view: 
tomjbr.16324861@bloglines.com - 30 Sep 2005 11:25 GMT
Is there any code examples on the web about how to reorder the
rows in a JTable (using a generic javax.swing.table.TableModel) by drag
and drop ?

I have found these two exampes:
http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/
http://forum.java.sun.com/thread.jspa?threadID=497065&messageID=2431829
but both of these are downcasting to a
javax.swing.table.DefaultTableModel
and I would like to see an implementation that works for any
TableModel.

Since I do not need features that enables dragging and dropping between
different GUI components, but just want to be able to move rows within
the same table, I am wondering if it still really is necessary to use
the packages java.awt.datatransfer and java.awt.dnd, or if it then can
be done in a less complicated way ?
(and if it can be easier, then a link to some nice source code would be
great, please)

/ Tom
tomjbr.16324861@bloglines.com - 30 Sep 2005 12:11 GMT
> I have found these two exampes:
> http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/
>...

By the way, it was the ExtendedDnDDemo example I was refering to at the
above page.

tomjbr.16324...@bloglines.com wrote:
> Is there any code examples on the web about how to reorder the
> rows in a JTable (using a generic javax.swing.table.TableModel) by drag
[quoted text clipped - 17 lines]
>
> / Tom
Roedy Green - 30 Sep 2005 22:35 GMT
>Since I do not need features that enables dragging and dropping between
>different GUI components, but just want to be able to move rows within
>the same table,

All that matters to you is the source row# and the target row #.  Once
you have that, you can do modify the TableModel Yourself.

Here is how I would proceed:

1. Take the code for a component to component drag and drop, and strip
out everything to do with dataflavours, the clipboard etc.  All you
want is the stuff that identifies source and target, the event handler
skeletons, anything you must do to mark potential targets...

2. Next you want to narrow it to a row within the JTable component.
First google for JTable DND Drag and Drop and see if there is any code
posted you could cannibalise.  Try searching for cut/copy/paste which
is similar.  I trust you have already tried this without success.
a) failing that, perhaps DND has something  to handle this.
b) failing that, perhaps JTable has something to do this
c) failing that,  perhaps some sort of get/selected method will do it
for you.
d) failing that, perhaps there is some method that will convert x,y to
row number for you.
e) failing that, write you own code to figure out row number.  You can
ignore scrolling since x,y are relative to the full component, not the
scrolled visible part.

Now you have to from and to row numbers, delete the source row in your
TableModel (easy presuming it is an ArrayList), insert the row at the
target row, then fire the delete and insert events
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.