Re: TransferHandler exportAsDone called before exportAsDrag returns
 
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 imple=
ment > drag-and-drop. I add a mouse listener to the component, and in > its=
 mousePressed() method I call > JComponent c = (JComponent)event.getSourc=
e(); > TransferHandler th = c.getTransferHandler(); > th.exportAsDrag( co=
mp, event, TransferHandler.COPY ); > > The problem is that the handler's ex=
portAsDone() method is > called immediately - before exportAsDrag() returns=
, and > before the mouse is moved or released. Can you provide an SSCCE? ht=
tp://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 Transfe=
rHandler, but my component did not have a "text" property.
For example, this does work (and works for "text" when I add a "text" prope=
rty 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