Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4225247

Drag-and-Drop does not work in an applet on Windows NT

    • 04
    • x86
    • solaris_2.6, windows_98, windows_nt
    • Verified

        m labelName - the heading for the list
        X * @param listModel - model for the list
        X */
        X
        X private JPanel getListPanel(DNDList list, String labelName, DefaultListModel
        listModel ){
        X JPanel listPanel = new JPanel();
        X JScrollPane scrollPane = new JScrollPane(list);
        X
        X list.setModel(listModel);
        X JLabel nameListName = new JLabel(labelName );
        X
        X listPanel.setLayout( new BorderLayout());
        X listPanel.add(nameListName, BorderLayout.NORTH);
        X listPanel.add( scrollPane, BorderLayout.CENTER);
        X
        X return listPanel;
        X }
        X
        }
        X
        SHAR_EOF
          $shar_touch -am 01311128100 'TestDND.java' &&
          chmod 0644 'TestDND.java' ||
          $echo 'restore of' 'TestDND.java' 'failed'
          if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
            md5sum -c << SHAR_EOF >/dev/null 2>&1 || $echo 'TestDND.java:' 'MD5 check failed'
        a1655b8058366e52f642edf0c8e45902 TestDND.java
        SHAR_EOF
          else
            shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'TestDND.java'`"
            test 3611 -eq "$shar_count" ||
            $echo 'TestDND.java:' 'original size' '3611,' 'current size' "$shar_count!"
          fi
        fi
        rm -fr _sh05514
        exit 0
        (Review ID: 100572)
        ======================================================================

        Name: jk109818 Date: 06/23/2000


        java version "1.3.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
        Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

        I am trying to implement DnD for an applet but I do not receive any messages in
        the class that implements DropTargetListener. If the code is moved in an
        application everithing works fine.

        The code was tested using the applet viewer.
        (Review ID: 104725)
        ======================================================================

        Name: jk109818 Date: 06/26/2000


        java version "1.2.2"
        Classic VM (build JDK-1.2.2-001, native threads, symcjit)

        package xmltest;
        import java.awt.*;
        import java.awt.event.*;
        import java.awt.datatransfer.*;
        import java.awt.dnd.*;
        import java.io.*;
        import javax.swing.*;

        public class DropApplet extends JApplet {

            static JTextArea textArea;

            public void init() {
        /*
                textArea = new JTextArea(20, 40);
                textArea.setLineWrap(true);
                JScrollPane scroller = new JScrollPane(textArea);
                getContentPane().add(scroller);
                textArea.setDropTarget(new DropTarget(textArea, new TextAreaTarget
        (textArea)));
        */
        startTest();
        }

        public void startTest(){
        JFrame frame = new JFrame("Drop on Me!");
                textArea = new JTextArea(20, 40);
                textArea.setLineWrap(true);
                JScrollPane scroller = new JScrollPane(textArea);
                frame.setContentPane(scroller);
                frame.pack();
                frame.show();
        DropThread dt = new DropThread(textArea);
        dt.start();
            }
        }

        class DropThread extends Thread{
          JTextArea textArea;
          public DropThread(JTextArea tArea){
        textArea = tArea;
        }
          public void run(){
        try{
        System.out.println(" Sleeping:");
        Thread.sleep(3000);
        }
        catch(Exception e){
        System.out.println(" here in thrd ex");
        }
        System.out.println(" Done Sleeping:");
        textArea.setDropTarget(new DropTarget(textArea, new TextAreaTarget
        (textArea)));
        System.out.println(" Done setting drop");
        }
        }

        class TextAreaTarget implements DropTargetListener {

            JTextArea textArea;

            public TextAreaTarget(JTextArea area) {
               textArea = area;
            }
            public void dragEnter(DropTargetDragEvent e) {
                System.err.println("[Target] dragEnter");

                e.acceptDrag(DnDConstants.ACTION_COPY);
            }

            public void dragOver(DropTargetDragEvent e) {
                e.acceptDrag(DnDConstants.ACTION_COPY);
                System.err.println("[Target] dragOver");
            }

            public void dragExit(DropTargetEvent e) {
                System.err.println("[Target] dragExit");
            }

            public void drop(DropTargetDropEvent e) {
                System.err.println("[Target] drop");
                DropTargetContext targetContext = e.getDropTargetContext();

                boolean outcome = false;

                if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0)
                    e.acceptDrop(DnDConstants.ACTION_COPY);
                else {
                    e.rejectDrop();
                    return;
                }

                DataFlavor[] dataFlavors = e.getCurrentDataFlavors();
                DataFlavor transferDataFlavor = null;

                System.err.println(DataFlavor.plainTextFlavor.getMimeType());
                for (int i = 0; i < dataFlavors.length; i++) {
                    System.err.println(dataFlavors[i].getMimeType());
                    if (DataFlavor.plainTextFlavor.equals(dataFlavors[i])) {
                        System.err.println("matched");
                        transferDataFlavor = dataFlavors[i];
                        break;
                    }
                }

                if (transferDataFlavor != null) {
                    Transferable t = e.getTransferable();
                    InputStream is = null;

                    try {
                        System.err.println("get stream");
                        is = (InputStream)t.getTransferData(transferDataFlavor);
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                        System.err.println(ioe.getMessage());
                        targetContext.dropComplete(false);

                        return;
                    } catch (UnsupportedFlavorException ufe) {
                        ufe.printStackTrace();
                        System.err.println(ufe.getMessage());
                        targetContext.dropComplete(false);

                        return;
                    }

                    if (is != null) {
                        try {

                            Reader converter = new InputStreamReader(is);
                            StringWriter sWriter = new StringWriter();
                            boolean more = true;
                            while (more) {
                                int c = converter.read();
                                if (c != -1) {
                                    sWriter.write(c);
                                } else {
                                    more = false;
                                }
                            }
                            textArea.append(sWriter.toString().trim());

                            outcome = true;
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            System.err.println(ex.getMessage());
                            targetContext.dropComplete(false);
                            return;
                        }
                    } else
                        outcome = false;
                }

                targetContext.dropComplete(outcome);
            }

            public void dragScroll(DropTargetDragEvent e) {
            }

            public void dropActionChanged(DropTargetDragEvent e) {
                System.err.println("[Target] dropActionChanged");
            }
        }
        (Review ID: 104679)
        ======================================================================

        Stand-alone test case is attached.
        david.mendenhall@eng 2000-08-01
        Drag-and-Drop does not work in an applet on Windows NT. It does not work with appletviewer nor with Java Plugin. Same classes work fine when used as an application.

        Steps to simulate the problem,

        1. The source and html files are located at /home/mukundm/bug/DnD
        2. Load the applet in the appletviewer using the file indexnoplugin.html
        3. Dnd text. It does not work.
        4. Run the test as an application - java TestText. DnD works fine here.

        Name: krT82822 Date: 02/03/2000


        java version "1.3.0rc1"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
        Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

        This bug report is identical to bug 4225247. I wanted to provide you with a
        working testcase and an update with JDK1.3RC1

        I tested the drag and drop sample at
        http://web2.java.sun.com/docs/books/tutorial/dnd/sheetal.html by converting it
        into an Applet. Under internet explorer, only the dragExit message is
        received. Under netscape, multiple dragexits are received and the test fails
        with a ClassCastException after a "drop"

        #!/bin/sh
        # This is a shell archive (produced by GNU sharutils 4.2).
        # To extract the files from this archive, save it to some FILE, remove
        # everything before the `!/bin/sh' line above, then type `sh FILE'.
        #
        # Existing files will *not* be overwritten unless `-c' is specified.
        #
        # This shar contains:
        # length mode name
        # ------ ---------- ------------------------------------------
        # 737 -rw-r--r-- Test.html
        # 119 -rw-r--r-- DNDComponentInterface.java
        # 4958 -rw-r--r-- DNDList.java
        # 3611 -rw-r--r-- TestDND.java
        #
        save_IFS="${IFS}"
        IFS="${IFS}:"
        gettext_dir=FAILED
        locale_dir=FAILED
        first_param="$1"
        for dir in $PATH
        do
          if test "$gettext_dir" = FAILED && test -f $dir/gettext && ($dir/gettext --version >/dev/null 2>&1)
          then
            set `$dir/gettext --version 2>&1`
            if test "$3" = GNU
            then
              gettext_dir=$dir
            fi
          fi
          if test "$locale_dir" = FAILED && test -f $dir/shar && ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
          then
            locale_dir=`$dir/shar --print-text-domain-dir`
          fi
        done
        IFS="$save_IFS"
        if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
        then
          echo=echo
        else
          TEXTDOMAINDIR=$locale_dir
          export TEXTDOMAINDIR
          TEXTDOMAIN=sharutils
          export TEXTDOMAIN
          echo="$gettext_dir/gettext -s"
        fi
        touch -am 1231235999 $$.touch >/dev/null 2>&1
        if test ! -f 1231235999 && test -f $$.touch; then
          shar_touch=touch
        else
          shar_touch=:
          echo
          $echo 'WARNING: not restoring timestamps. Consider getting and'
          $echo "installing GNU \`touch', distributed in GNU File Utilities..."
          echo
        fi
        rm -f 1231235999 $$.touch
        #
        if mkdir _sh05514; then
          $echo 'x -' 'creating lock directory'
        else
          $echo 'failed to create lock directory'
          exit 1
        fi
        # ============= Test.html ==============
        if test -f 'Test.html' && test "$first_param" != -c; then
          $echo 'x -' SKIPPING 'Test.html' '(file already exists)'
        else
          $echo 'x -' extracting 'Test.html' '(text)'
          sed 's/^X//' << 'SHAR_EOF' > 'Test.html' &&
        <html>
        <body>
        X
        <!-- Internet Explorer OBJECT -->
        <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        X width=100% height=100% align="baseline"
        X CODEBASE="http://java.sun.com/products/plugin/1.2/jinstall-12-
        win32.cab#Version=1,2,0,0">
        X <PARAM NAME="code" VALUE="TestDND">
        X <PARAM NAME="type" VALUE="application/x-java-applet;version=1.2">
        X <COMMENT>
        X
        X <!-- Netscape EMBED -->
        X
        X <EMBED type="application/x-java-applet;version=1.2" width=100%
        X height=100% align="baseline"
        X code="TestDND"
        X pluginspage="http://java.sun.com/products/plugin/1.2/plugin-
        install.html">
        X <NOEMBED>
        X No JDK 1.2 support for APPLET!!
        X </NOEMBED>
        X </EMBED>
        X </COMMENT>
        </OBJECT>
        X
        X
        </body>
        </html>
        SHAR_EOF
          $shar_touch -am 01311128100 'Test.html' &&
          chmod 0644 'Test.html' ||
          $echo 'restore of' 'Test.html' 'failed'
          if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
            md5sum -c << SHAR_EOF >/dev/null 2>&1 || $echo 'Test.html:' 'MD5 check failed'
        b78d667c44cb23540473d1364ea17142 Test.html
        SHAR_EOF
          else
            shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'Test.html'`"
            test 737 -eq "$shar_count" ||
            $echo 'Test.html:' 'original size' '737,' 'current size' "$shar_count!"
          fi
        fi
        # ============= DNDComponentInterface.java ==============
        if test -f 'DNDComponentInterface.java' && test "$first_param" != -c; then
          $echo 'x -' SKIPPING 'DNDComponentInterface.java' '(file already exists)'
        else
          $echo 'x -' extracting 'DNDComponentInterface.java' '(text)'
          sed 's/^X//' << 'SHAR_EOF' > 'DNDComponentInterface.java' &&
        public interface DNDComponentInterface{
        X
        X public void addElement( Object s);
        X public void removeElement();
        X
        }
        X
        X
        SHAR_EOF
          $shar_touch -am 01311128100 'DNDComponentInterface.java' &&
          chmod 0644 'DNDComponentInterface.java' ||
          $echo 'restore of' 'DNDComponentInterface.java' 'failed'
          if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
            md5sum -c << SHAR_EOF >/dev/null 2>&1 || $echo 'DNDComponentInterface.java:' 'MD5 check failed'
        f581b238e9ea9f2d964e9094771f8759 DNDComponentInterface.java
        SHAR_EOF
          else
            shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'DNDComponentInterface.java'`"
            test 119 -eq "$shar_count" ||
            $echo 'DNDComponentInterface.java:' 'original size' '119,' 'current
        size' "$shar_count!"
          fi
        fi
        # ============= DNDList.java ==============
        if test -f 'DNDList.java' && test "$first_param" != -c; then
          $echo 'x -' SKIPPING 'DNDList.java' '(file already exists)'
        else
          $echo 'x -' extracting 'DNDList.java' '(text)'
          sed 's/^X//' << 'SHAR_EOF' > 'DNDList.java' &&
        /**
        X * This is an example of a component, which serves as a DragSource as
        X * well as Drop Target.
        X * To illustrate the concept, JList has been used as a droppable target
        X * and a draggable source.
        X * Any component can be used instead of a JList.
        X * The code also contains debugging messages which can be used for
        X * diagnostics and understanding the flow of events.
        X *
        X * @version 1.0
        X */
        X
        import java.awt.*;
        import java.awt.dnd.*;
        import java.awt.datatransfer.*;
        X
        import java.util.Hashtable;
        import java.util.List;
        import java.util.Iterator;
        X
        import java.io.*;
        import java.io.IOException;
        X
        import javax.swing.JList;
        import javax.swing.DefaultListModel;
        X
        X
        X
        X
        public class DNDList extends JList
        X implements DNDComponentInterface, DropTargetListener,DragSourceListener,
        DragGestureListener {
        X
        X /**
        X * enables this component to be a dropTarget
        X */
        X
        X DropTarget dropTarget = null;
        X
        X /**
        X * enables this component to be a Drag Source
        X */
        X DragSource dragSource = null;
        X
        X
        X /**
        X * constructor - initializes the DropTarget and DragSource.
        X */
        X
        X public DNDList() {
        X
        X dropTarget = new DropTarget (this, this);
        X dragSource = new DragSource();
        X dragSource.createDefaultDragGestureRecognizer( this,
        DnDConstants.ACTION_MOVE, this);
        X }
        X
        X /**
        X * is invoked when you are dragging over the DropSite
        X *
        X */
        X
        X public void dragEnter (DropTargetDragEvent event) {
        X
        X // debug messages for diagnostics
        X System.out.println( "dragEnter");
        X event.acceptDrag (DnDConstants.ACTION_MOVE);
        X }
        X
        X /**
        X * is invoked when you are exit the DropSite without dropping
        X *
        X */
        X
        X public void dragExit (DropTargetEvent event) {
        X System.out.println( "dragExit");
        X
        X }
        X
        X /**
        X * is invoked when a drag operation is going on
        X *
        X */
        X
        X public void dragOver (DropTargetDragEvent event) {
        X System.out.println( "dragOver");
        X }
        X
        X /**
        X * a drop has occurred
        X *
        X */
        X
        X
        X public void drop (DropTargetDropEvent event) {
        X
        X try {
        X Transferable transferable = event.getTransferable();
        X
        X // we accept only Strings
        X if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
        X
        X event.acceptDrop(DnDConstants.ACTION_MOVE);
        X String s = (String)transferable.getTransferData (
        DataFlavor.stringFlavor);
        X addElement( s );
        X event.getDropTargetContext().dropComplete(true);
        X }
        X else{
        X event.rejectDrop();
        X }
        X }
        X catch (IOException exception) {
        X exception.printStackTrace();
        X System.err.println( "Exception" + exception.getMessage());
        X event.rejectDrop();
        X }
        X catch (UnsupportedFlavorException ufException ) {
        X ufException.printStackTrace();
        X System.err.println( "Exception" + ufException.getMessage());
        X event.rejectDrop();
        X }
        X }
        X
        X /**
        X * is invoked if the use modifies the current drop gesture
        X *
        X */
        X
        X
        X public void dropActionChanged ( DropTargetDragEvent event ) {
        X }
        X
        X /**
        X * a drag gesture has been initiated
        X *
        X */
        X
        X public void dragGestureRecognized( DragGestureEvent event) {
        X
        X Object selected = getSelectedValue();
        X if ( selected != null ){
        X StringSelection text = new StringSelection( selected.toString());
        X
        X // as the name suggests, starts the dragging
        X dragSource.startDrag (event, DragSource.DefaultMoveDrop, text, this);
        X } else {
        X System.out.println( "nothing was selected");
        X }
        X }
        X
        X /**
        X * this message goes to DragSourceListener, informing it that the dragging
        X * has ended
        X *
        X */
        X
        X public void dragDropEnd (DragSourceDropEvent event) {
        X if ( event.getDropSuccess()){
        X removeElement();
        X }
        X }
        X
        X /**
        X * this message goes to DragSourceListener, informing it that the dragging
        X * has entered the DropSite
        X *
        X */
        X
        X public void dragEnter (DragSourceDragEvent event) {
        X System.out.println( " dragEnter");
        X }
        X
        X /**
        X * this message goes to DragSourceListener, informing it that the dragging
        X * has exited the DropSite
        X *
        X */
        X
        X public void dragExit (DragSourceEvent event) {
        X System.out.println( "dragExit");
        X
        X }
        X
        X /**
        X * this message goes to DragSourceListener, informing it that the dragging
        is currently
        X * ocurring over the DropSite
        X *
        X */
        X
        X public void dragOver (DragSourceDragEvent event) {
        X System.out.println( "dragExit");
        X
        X }
        X
        X /**
        X * is invoked when the user changes the dropAction
        X *
        X */
        X
        X public void dropActionChanged ( DragSourceDragEvent event) {
        X System.out.println( "dropActionChanged");
        X }
        X
        X /**
        X * adds elements to itself
        X *
        X */
        X
        X public void addElement( Object s ){
        X (( DefaultListModel )getModel()).addElement (s.toString());
        X }
        X
        X /**
        X * removes an element from itself
        X */
        X
        X public void removeElement(){
        X (( DefaultListModel)getModel()).removeElement( getSelectedValue());
        X }
        X
        }
        X
        SHAR_EOF
          $shar_touch -am 01311128100 'DNDList.java' &&
          chmod 0644 'DNDList.java' ||
          $echo 'restore of' 'DNDList.java' 'failed'
          if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
            md5sum -c << SHAR_EOF >/dev/null 2>&1 || $echo 'DNDList.java:' 'MD5 check failed'
        b9fe1aefe0fd47f98b6b12dfd03be047 DNDList.java
        SHAR_EOF
          else
            shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'DNDList.java'`"
            test 4958 -eq "$shar_count" ||
            $echo 'DNDList.java:' 'original size' '4958,' 'current size' "$shar_count!"
          fi
        fi
        # ============= TestDND.java ==============
        if test -f 'TestDND.java' && test "$first_param" != -c; then
          $echo 'x -' SKIPPING 'TestDND.java' '(file already exists)'
        else
          $echo 'x -' extracting 'TestDND.java' '(text)'
          sed 's/^X//' << 'SHAR_EOF' > 'TestDND.java' &&
        /**
        X * The tester class for the DNDList. This class creates the lists,
        X * positions them in a frame, populates the list with the default
        X * data.
        X *
        X * @version 1.0
        X *
        X */
        X
        import java.awt.*;
        import java.awt.event.*;
        X
        import javax.swing.*;
        X
        X
        public class TestDND extends JApplet
        {
        X public static void main (String args[]) {
        X new TestDND().initApplication();
        }
        X
        X /**
        X * constructor
        X * creates the frame, the lists in it and sets the data in the lists
        X */
        X
        X public void initApplication()
        X {
        X JFrame f = new JFrame("Drag and Drop Lists");
        X
        X DNDList sourceList = new DNDList();
        X
        X // add data to the source List
        X DefaultListModel sourceModel = new DefaultListModel();
        X
        X sourceModel.addElement( "Source Item1");
        X sourceModel.addElement( "Source Item2");
        X sourceModel.addElement( "Source Item3");
        X sourceModel.addElement( "Source Item4");
        X
        X // gets the panel with the List and a heading for the List
        X JPanel sourcePanel = getListPanel(sourceList, "SourceList", sourceModel);
        X
        X DNDList targetList = new DNDList();
        X
        X // add data to the target List
        X DefaultListModel targetModel = new DefaultListModel();
        X
        X targetModel.addElement( "Target Item1");
        X targetModel.addElement( "Target Item2");
        X targetModel.addElement( "Target Item3");
        X targetModel.addElement( "Target Item4");
        X
        X JPanel targetPanel = getListPanel(targetList, "TargetList", targetModel);
        X JPanel mainPanel = new JPanel();
        X mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
        X mainPanel.add( sourcePanel );
        X mainPanel.add( targetPanel );
        X
        X f.getContentPane().add( mainPanel );
        X f.setSize (300, 300);
        X f.addWindowListener (new WindowAdapter() {
        X public void windowClosing(WindowEvent e) {
        X System.exit(0);
        X }
        X });
        X f.setVisible (true);
        X }
        X
        X public void init ()
        X {
        X DNDList sourceList = new DNDList();
        X
        X // add data to the source List
        X DefaultListModel sourceModel = new DefaultListModel();
        X
        X sourceModel.addElement( "Source Item1");
        X sourceModel.addElement( "Source Item2");
        X sourceModel.addElement( "Source Item3");
        X sourceModel.addElement( "Source Item4");
        X
        X // gets the panel with the List and a heading for the List
        X JPanel sourcePanel = getListPanel(sourceList, "SourceList", sourceModel);
        X
        X DNDList targetList = new DNDList();
        X
        X // add data to the target List
        X DefaultListModel targetModel = new DefaultListModel();
        X
        X targetModel.addElement( "Target Item1");
        X targetModel.addElement( "Target Item2");
        X targetModel.addElement( "Target Item3");
        X targetModel.addElement( "Target Item4");
        X
        X JPanel targetPanel = getListPanel(targetList, "TargetList", targetModel);
        X JPanel mainPanel = new JPanel();
        X mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
        X mainPanel.add( sourcePanel );
        X mainPanel.add( targetPanel );
        X
        X getContentPane().add( mainPanel );
        X }
        X
        X /**
        X * a convenience method
        X * used for positioning of the ListBox and the Label.
        X *
        X * @param list - the special DND List
        X * @para

              dmendenhsunw David Mendenhall (Inactive)
              mmadhugisunw Mukund Madhugiri (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: