hi
i have a applet with 30 to 40 JLists which has images on it.
i am able to drag the image but not able to drop it..
i'll paste both the applet as well as the class file..
i have been trying this in appletviewer...
i have another problem when iam trying to use this in browser..
the images are not getting displayed...
it is giving access control exception
can u pls help me in solving this.
the applet
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestDNDNew extends JApplet{
DefaultListModel model[]=null;
DNDListNew1 list[]=null;
JPanel panel[]=null;
Vector track=new Vector();
JFrame f=null;
JPanel mainPanel = new JPanel();
GridLayout gridbag = new GridLayout(12,13);
public TestDNDNew(){
}
public void init(){
GridLayout gridbag = new GridLayout(12,13);
model=new DefaultListModel[156];
list=new DNDListNew1[156];
panel=new JPanel[156];
for(int i=0;i<156;i++){
list[i]= new DNDListNew1(i);
model[i] = new DefaultListModel();
panel[i] = getListPanel(list[i], model[i]);
mainPanel.add(panel[i]);
}
mainPanel.setLayout(gridbag);
getContentPane().add( mainPanel );
}
public void start(){
ImageIcon img= new ImageIcon("1a.gif");
Object obj=(ImageIcon)img;
model[1].add(0,obj);
}
private JPanel getListPanel(DNDListNew1 list, DefaultListModel listModel ){
JPanel listPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(list);
list.setModel(listModel);
list.setLayout(new GridLayout(2,2));
listPanel.setLayout( new BorderLayout());
//listPanel.setLayout( new GridLayout(2,2));
listPanel.add( scrollPane, BorderLayout.CENTER);
return listPanel;
}
public static void main (String args[]) {
TestDNDNew testDND = new TestDNDNew();
}
}
the class file
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.util.Hashtable;
import java.util.List;
import java.util.Iterator;
import java.io.*;
import java.io.IOException;
import javax.swing.JList;
import javax.swing.DefaultListModel;
public class DNDListNew1 extends JList
implements DNDComponentInterface, DropTargetListener,DragSourceListener, DragGestureListener {
/**
* enables this component to be a dropTarget
*/
DropTarget dropTarget = null;
int value=0;
/**
* enables this component to be a Drag Source
*/
DragSource dragSource = null;
/**
* constructor - initializes the DropTarget and DragSource.
*/
public DNDListNew1(int i) {
value =i;
// dropTarget = new DropTarget (this, this);
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
}
/**
* is invoked when you are dragging over the DropSite
*
*/
public void dragEnter (DropTargetDragEvent event) {
// debug messages for diagnostics
System.out.println( "dragEnter");
event.acceptDrag (DnDConstants.ACTION_MOVE);
}
/**
* is invoked when you are exit the DropSite without dropping
*
*/
public void dragExit (DropTargetEvent event) {
System.out.println( "dragExit "+value);
}
/**
* is invoked when a drag operation is going on
*
*/
public void dragOver (DropTargetDragEvent event) {
System.out.println( "dragOver");
}
/**
* a drop has occurred
*
*/
public void drop (DropTargetDropEvent event) {
System.out.println("hello");
try {
Transferable transferable = event.getTransferable();
// we accept only Strings
if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
event.acceptDrop(DnDConstants.ACTION_MOVE);
String s = (String)transferable.getTransferData ( DataFlavor.stringFlavor);
String img=s.toString();
System.out.println(" image "+img);
//Object o=new ImageIcon (img);
addElement( s );
// addElement( s );
event.getDropTargetContext().dropComplete(true);
}
else{
event.rejectDrop();
}
}
catch (IOException exception) {
exception.printStackTrace();
System.err.println( "Exception" + exception.getMessage());
event.rejectDrop();
}
catch (UnsupportedFlavorException ufException ) {
ufException.printStackTrace();
System.err.println( "Exception" + ufException.getMessage());
event.rejectDrop();
}
}
/**
* is invoked if the use modifies the current drop gesture
*
*/
public void dropActionChanged ( DropTargetDragEvent event ) {
}
/**
* a drag gesture has been initiated
*
*/
public void dragGestureRecognized( DragGestureEvent event) {
Object selected = getSelectedValue();
if ( selected != null ){
System.out.println("value of drag enter is "+value);
StringSelection text = new StringSelection( selected.toString());
// as the name suggests, starts the dragging
System.out.println(" image selected "+selected);
dragSource.startDrag (event, DragSource.DefaultMoveDrop, text, this);
System.out.println(DragSource.DefaultMoveDrop);
System.out.println(" text "+text);
} else {
System.out.println( "nothing was selected");
}
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has ended
*
*/
public void dragDropEnd (DragSourceDropEvent event) {
if ( event.getDropSuccess()){
removeElement();
}
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has entered the DropSite
*
*/
public void dragEnter (DragSourceDragEvent event) {
System.out.println( " dragEnter");
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has exited the DropSite
*
*/
public void dragExit (DragSourceEvent event) {
System.out.println( "dragExit "+value);
}
/**
* this message goes to DragSourceListener, informing it that the dragging is currently
* ocurring over the DropSite
*
*/
public void dragOver (DragSourceDragEvent event) {
System.out.println( "dragExit ");
}
/**
* is invoked when the user changes the dropAction
*
*/
public void dropActionChanged ( DragSourceDragEvent event) {
System.out.println( "dropActionChanged");
}
/**
* adds elements to itself
*
*/
public void addElement( Object s ){
(( DefaultListModel )getModel()).addElement (s.toString());
}
/**
* removes an element from itself
*/
public void removeElement(){
(( DefaultListModel)getModel()).removeElement( getSelectedValue());
}
}
interface
public interface DNDComponentInterface{
public void addElement( Object s);
public void removeElement();
}
the html
<html>
<head></head>
<body>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
WIDTH =980 HEIGHT = 580 codebase="
http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<PARAM NAME = CODE VALUE = "TestDNDNew.class" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">
</OBJECT>
</body>
</html>
with regards
sulu