Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TransferHandler exportAsDone called before exportAsDrag returns

91 views
Skip to first unread message

FredK

unread,
Jun 10, 2013, 11:44:11 AM6/10/13
to
I have a subclass of JPanel for which I am trying to implement
drag-and-drop. I add a mouse listener to the component, and in
its mousePressed() method I call
JComponent c = (JComponent)event.getSource();
TransferHandler th = c.getTransferHandler();
th.exportAsDrag( comp, event, TransferHandler.COPY );

The problem is that the handler's exportAsDone() method is
called immediately - before exportAsDrag() returns, and
before the mouse is moved or released.

--
Fred K

Lew

unread,
Jun 10, 2013, 6:14:11 PM6/10/13
to
Can you provide an SSCCE?

http://sscce.org/

The problem appears to be in the part of the code you haven't shown us.

Are you managing events on the EDT properly?

--
Lew

FredK

unread,
Jun 11, 2013, 10:44:35 AM6/11/13
to
On Monday, June 10, 2013 3:14:11 PM UTC-7, Lew wrote:
> FredK wrote: > I have a subclass of JPanel for which I am trying to implement > drag-and-drop. I add a mouse listener to the component, and in > its mousePressed() method I call > JComponent c = (JComponent)event.getSource(); > TransferHandler th = c.getTransferHandler(); > th.exportAsDrag( comp, event, TransferHandler.COPY ); > > The problem is that the handler's exportAsDone() method is > called immediately - before exportAsDrag() returns, and > before the mouse is moved or released. Can you provide an SSCCE? http://sscce.org/ The problem appears to be in the part of the code you haven't shown us. Are you managing events on the EDT properly? -- Lew


I hadn't realized that JPanel (or I guess JComponent) had built-in DnD for bean properties - didn't see any mention of it in the Javadoc tutorials.
I now have it working - I had been using "text" as the argument for TransferHandler, but my component did not have a "text" property.

For example, this does work (and works for "text" when I add a "text" property to my panel):

package boeing.geoduck.viewer.swingitems;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;

public class DragTest {

public static void main( String args[] ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 300, 150 );

JPanel w = new JPanel();
frame.add( w, BorderLayout.CENTER );

w.setTransferHandler( new TransferHandler( "background" ) );

w.addMouseListener( new MouseAdapter() {
@Override
public void mousePressed( MouseEvent me ) {
JComponent comp = (JComponent) me.getSource();
TransferHandler handler = comp.getTransferHandler();
int actions = handler.getSourceActions( comp );
System.out.println( actions );
handler.exportAsDrag( comp, me, TransferHandler.COPY );
}
} );

frame.setVisible( true );
}
} );
}
}

--
Fred K
0 new messages