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 / June 2004

Tip: Looking for answers? Try searching our database.

JTable drag-and-drop

Thread view: 
dar7yl - 27 Jun 2004 18:59 GMT
I have a JTable set up for drag-and-drop, but the default behavior does not
please me, nor my clients.

 myTable.setRowSelectionAllowed(true);
 myTable.setTransferHandler(CT_Handler);
 myTable.setDragEnabled(true);

The problem is that a row must be selected before d&d becomes active.  That
means that the honorable user must click on the table first, then click
again to drag the row.  This behavior is not consistant with the prevailing
operation of common gui interfaces (read: MFC, VB).

How can I change the behavior to combine the click, select and drag into one
operation?

I've tried the following, to no avail:

public void mousePressed( MouseEvent e )
{
     Object src = e.getSource();

     if (src == myTable)
     {
          CT_Handler.exportAsDrag(
              (JComponent) src, (InputEvent) e,
              TransferHandler.COPY );
     }
}

regards,
   Dar7yl    (the 7 is silent)
SPC - 28 Jun 2004 17:06 GMT
In short, you can't. Not by setting an option anyway. There's a
longstanding bug on Suns database. Included in the comments on the bug
there are a couple of suggestions for changing the behaviour of JTable
to get this to work.

See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4521075

Essentially, you have derive from the current table UI, uninstall
their drag gesture listener (a mouse listener which refuses to start a
drag on an unselected row) and install one of your own which *will*
start a drag under those circumstances. Just take a copy of the UI
code for the JTable drag recognizer, modify it and install it instead.
If you do this, then selection won't move because the recognizer
consumes the mouse event, and it never gets to the selection code. See
the comment by 'scmorr', in his? case, he? wanted the dragged row to
be selected, so reordering the listeners was enough. In my case I
*didn't* want to select the dragged row, so I had to do more work.

Unfortunately, the code's not mine (though I wrote it) so I can't post
it...

HTH

Steve

> I have a JTable set up for drag-and-drop, but the default behavior does not
> please me, nor my clients.
[quoted text clipped - 27 lines]
> regards,
>     Dar7yl    (the 7 is silent)
dar7yl - 28 Jun 2004 19:26 GMT
> In short, you can't. Not by setting an option anyway. There's a
> longstanding bug on Suns database. Included in the comments on the bug
> there are a couple of suggestions for changing the behaviour of JTable
> to get this to work.
>
> See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4521075

Thanks for the link.  It sure is entertaining reading.  I agree that Sun is
most negligent in their approach to outstanding defects.  You'd think that
with the attention this bug report has generated, it would be high on the
priority list.  At least they could publish a work-around until the "real"
fix comes through the pipeline.

> Essentially, you have derive from the current table UI, uninstall
> their drag gesture listener (a mouse listener which refuses to start a
> drag on an unselected row) and install one of your own which *will*
> start a drag under those circumstances. Just take a copy of the UI
> code for the JTable drag recognizer, modify it and install it instead.

What??  You mean I have to find some OPP (other people's problem) code,
decipher it, learn about some archaic computer-science-class concepts, fix
the problem, integrate it into my real-world application, document it, trace
it, and later remove it when Sun finally gets around to fixing it?

> If you do this, then selection won't move because the recognizer
> consumes the mouse event, and it never gets to the selection code. See
[quoted text clipped - 4 lines]
> Unfortunately, the code's not mine (though I wrote it) so I can't post
> it...

Thanks, but can you be a bit more specific?  You could, of course retype it
from memory without violating your employer's NDA.  -- or better still, ask
them politely (via letter) for permisssion to publish that portion of code.
(yea, I know, lots of work, can of worms...)

thanx,
   Dar7yl    (the 7 is silent)
SPC - 29 Jun 2004 14:51 GMT
Ever heard the one about not looking a gift horse in the mouth? :-)

I'll see what I can do. I have to extract the code from some other
stuff I also put into the UI to make it work better with a specific
use of JTreeTable that my employer required. One side effect of my
changes is that dragging to select rows and columns no-longer works.
I'm sure you'll be able to unpick what you need from the code. But
it'll have to wait until I've got a bit more time. (days rather than
weeks or months!).

What's depressing it that they apparently have no plans to change the
thing so that it behaves the same as many other windows tables on
windows. It wouldn't be beyond the wit of man to put in some sort of
property that governed drag operations and let the person using the
table decide how it should behave. Ho hum.

Steve

> > In short, you can't. Not by setting an option anyway. There's a
> > longstanding bug on Suns database. Included in the comments on the bug
[quoted text clipped - 36 lines]
> thanx,
>     Dar7yl    (the 7 is silent)
dar7yl - 30 Jun 2004 05:44 GMT
thanx again,
    Dar7yl    (the 7 is silent)

> Ever heard the one about not looking a gift horse in the mouth? :-)


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.