Darg and Drop Problem !

Hello I have a problem with Drag and Drop. In my Programm i have rooms(DockingFrames) and i want to represent some ImageComponents on this Frame -> class ImageComponent extends Component
I put my ImageComponents on this Frame and i put backgrond on this dockable like this:

		
	 private EditorLayout itsLayout=null;
		
		public EditorDockable( EditorFactory factory, EditorLayout layout ){
			/* it is mandatory to set the factory, the EditorDockable cannot be created 
			 * without it. */
			super( factory );
			this.itsLayout=layout;
			
			
			/* and then we just set up the editor */
			setTitleText( layout.getFileName() );
			
			if(layout.getFileContent()!=null){
				for(ImageComponent e: layout.getFileContent()){
					e.setParentDockable(this);
					add(e);
				}
			}
			add(layout.getBackground());

		}
		
	
		/* This convenient method allows us to grab the entire content of this dockable
		 * in one step. */
		public EditorLayout getLayout(){
			
			return itsLayout;
		}
	}```

now I would like to drag this ImageComponents to other DockingFrame
i Have a control class and create a new Room like this :

```public void createDockable(String roomName , ArrayList<DataComputer> dataList){
		 //Create GUI Components 
        ArrayList <ImageComponent>pc=new ArrayList<ImageComponent>();
		for(DataComputer e:dataList){
			 e.setGuiComponent(new  ImageComponent(e,img,this));
			 pc.add(e.getGuiComponent());
			
		}
	
		//Erstelle neuen Raum 
		 CLocation location = CLocation.base().normalNorth( 0.7).east( 0.76 ).stack( 0 );
		EditorDockable tempEditor= new EditorDockable( factory, new EditorLayout( roomName ,  pc ,new RaumPanel()  )  );
		tempEditor.setLocation(location);
		control.addDockable(tempEditor);
		//DnD
		this.dropTragets.add(new DropTargetDockable(tempEditor, DnDConstants.ACTION_MOVE, dndListener));
		// Add PC's
		for(int i=0;i<pc.size();i++){
			
		this.dragGestureRecognizers.add( dragSource.createDefaultDragGestureRecognizer(pc.get(i),  DnDConstants.ACTION_MOVE, dndListener));
		
		}
		updateTree("hirarchie");
		tempEditor.setCloseable(true);
		tempEditor.setVisible(true);
	}``` 
The problem is that i have my imageComponent not on a Panel but on the EditorDockable thats why i overwrite class DropTarget :

public class DropTargetDockable extends DropTarget {

private EditorDockable container=null;

public DropTargetDockable(EditorDockable container, int ops, DropTargetListener dtl ){
	
	
	
	this.container=container;
	setDefaultActions(ops);
	try {
		
		addDropTargetListener( dtl);
	} catch (TooManyListenersException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

public EditorDockable getDockable(){
	return this.container;
}

}```

and I have Dragand Drop class

import java.awt.datatransfer.*;
import java.awt.dnd.*;
public class DragDrop implements DragGestureListener, 
DragSourceListener,
        DropTargetListener, Transferable {
    static final DataFlavor[] supportedFlavors = {null};
    
    static {
        try {
            supportedFlavors[0] = new 
            DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Object object;
    // Transferable methods.
    public Object getTransferData(DataFlavor flavor) {
        if (flavor.isMimeTypeEqual
            (DataFlavor.javaJVMLocalObjectMimeType)) {
            return object;
        } else {
        	
            return null;
        }
    }
    public DataFlavor[] getTransferDataFlavors() {
        return supportedFlavors;
    }
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.isMimeTypeEqual
        (DataFlavor.javaJVMLocalObjectMimeType);
    }
    // DragGestureListener method.
    public void dragGestureRecognized(DragGestureEvent ev) {
    	System.out.println("start dnd "+ev.getSource());
    	   ev.startDrag(null, this, this);
    }
    // DragSourceListener methods.
    public void dragDropEnd(DragSourceDropEvent ev) {
    }
    public void dragEnter(DragSourceDragEvent ev) {
    }
    public void dragExit(DragSourceEvent ev) {
    }
    public void dragOver(DragSourceDragEvent ev) {
    	
        object = ev.getSource();
    }
    public void dropActionChanged(DragSourceDragEvent ev) {
    }
    // DropTargetListener methods.
    public void dragEnter(DropTargetDragEvent ev) {
    }
    public void dragExit(DropTargetEvent ev) {
    }
    public void dragOver(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    public void dropActionChanged(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    void dropTargetDrag(DropTargetDragEvent ev) {
        ev.acceptDrag(ev.getDropAction());
    }
    public void drop(DropTargetDropEvent ev) {
        ev.acceptDrop(ev.getDropAction());
        try {
            Object target = ev.getSource();
            Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);
            //ImageComponet
            Component component = ((DragSourceContext)source).getComponent();
            ImageComponent  imgComponent=(ImageComponent)component;
            EditorDockable oldEditor = imgComponent.getParentDockable();
            
            EditorDockable newEditor =  ((DropTargetDockable)target).getDockable();
           
           newEditor.add(imgComponent);
           oldEditor.getContentPane().validate();
           oldEditor.getContentPane().repaint();
           newEditor.getContentPane().validate();
           newEditor.getContentPane().repaint();
          
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        ev.dropComplete(true);
    }
    
}```

but IT DOESNT WORK ! why ? what im doing wron ?:confused:

DropTargetDockable

Don’t you have to call „setComponent“? Now the DropTargetDockable is not associated with any component, so it will never respond. „container.getContentPane“ might be the correct argument for „setComponent“.

yeah u a right but than i dont need to overwrite DaragTarget :slight_smile: can write than dockable.getContentPane
but i have another problem if i put it on the target container in DnD :

            Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);
            //ImageComponet
            Component component = ((DragSourceContext)source).getComponent();
            ImageComponent  imgComponent=(ImageComponent)component;
            EditorDockable oldEditor = imgComponent.getParentDockable();
            
            Container container = (Container) ((DropTarget) 
                    target).getComponent();
           
           container.add(imgComponent);
           oldEditor.getContentPane().validate();
           oldEditor.getContentPane().repaint();
           container.validate();
           container.repaint();```

the component is putting on the "wron place"  in this container  , its not above the  Background Panel but next to it and fill the most part of the frame hm and i cant see the icon just grey

So DnD is basically working, but the layout is wrong? Or have I misunderstood you?
What LayoutManager does “container” have? Maybe you need to set it to BorderLayout and use “add(imgComponent,BorderLayout.CENTER)”.

In any case: would it not be better to move around the image instead of the image-Component?

[QUOTE=Beni]So DnD is basically working, but the layout is wrong? Or have I misunderstood you?
What LayoutManager does „container“ have? Maybe you need to set it to BorderLayout and use „add(imgComponent,BorderLayout.CENTER)“.

In any case: would it not be better to move around the image instead of the image-Component?[/QUOTE]

yes DnD works but i stll have problems wioth layout , hm porder Layout doesnt work !
have java.awt.BorderLayout[hgap=0,vgap=0] layout but cant create new borderLayout(0,0) :frowning:
and the Editordockable has layout the class EditorLayout

If it already has a BorderLayout, then you don’t need a new instance. I do not have a better idea than “add(imgComponent,BorderLayout.CENTER)” at the moment, but I’ll copy your code and let it run in the evening (I’m currently at the office…). So hopefully I’m able to give a better answer in a few hours.

ok thx if u need more code say it :wink:

I’m not sure how close my test is to your application, but I don’t see any issues with the layout. Maybe you can enhance the test to show me what is going wrong?


import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.TooManyListenersException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.DefaultMultipleCDockable;
import bibliothek.gui.dock.common.MultipleCDockableFactory;
import bibliothek.gui.dock.common.MultipleCDockableLayout;
import bibliothek.gui.dock.control.relocator.DockRelocatorEvent;
import bibliothek.gui.dock.control.relocator.VetoableDockRelocatorListener;
import bibliothek.util.xml.XElement;

public class DragAndDropTest {
	public static void main( String[] args ){
		JFrame frame = new JFrame( "Test" );
		CControl control = new CControl( frame );
		frame.add( control.getContentArea() );

		EditorFactory factory = new EditorFactory();
		control.addMultipleDockableFactory( "editor", factory );
		EditorDockable left = new EditorDockable( factory, new EditorLayout() );
		EditorDockable right = new EditorDockable( factory, new EditorLayout() );

		control.addDockable( left );
		control.addDockable( right );

		left.setVisible( true );
		left.setTitleText( "Left" );

		right.setLocation( CLocation.base().normalEast( 0.5 ) );
		right.setVisible( true );
		right.getContentPane().add( new ImageComponent( right ) );
		right.setTitleText( "Right" );
		
		DragSource dragSource = new DragSource();
		DragDrop dndListener = new DragDrop( control );
		initDockable( left, dragSource, dndListener );
		initDockable( right, dragSource, dndListener );

		frame.setBounds( 20, 20, 500, 400 );
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.setVisible( true );
	}
	
	public static void initDockable(EditorDockable editor, DragSource dragSource, DragDrop dndListener){
		DropTargetDockable target = new DropTargetDockable(editor, DnDConstants.ACTION_MOVE, dndListener);
		dragSource.createDefaultDragGestureRecognizer( editor.getContentPane(), DnDConstants.ACTION_MOVE, dndListener );
	}
	
	public static class ImageComponent extends JPanel implements Transferable{
		private EditorDockable dockable;
		
		public ImageComponent( EditorDockable editor ){
			this.dockable = editor;
		}

		public void setParentDockable( EditorDockable dockable ){
			this.dockable = dockable;
		}

		public EditorDockable getParentDockable(){
			return dockable;
		}

		@Override
		public void paint( Graphics g ){
			super.paint( g );
			g.setColor( Color.RED );
			g.fillOval( 0, 0, getWidth(), getHeight() );
		}

		@Override
		public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException{
			return this;
		}

		@Override
		public DataFlavor[] getTransferDataFlavors(){
			try {
				return new DataFlavor[]{ new DataFlavor( DataFlavor.javaJVMLocalObjectMimeType ) };
			}
			catch( ClassNotFoundException e ) {
				throw new IllegalStateException( e );
			}
		}

		@Override
		public boolean isDataFlavorSupported( DataFlavor flavor ){
			return flavor.isMimeTypeEqual( DataFlavor.javaJVMLocalObjectMimeType );
		}
	}

	public static class EditorFactory implements MultipleCDockableFactory<EditorDockable, EditorLayout>{
		@Override
		public EditorLayout create(){
			return new EditorLayout();
		}

		@Override
		public boolean match( EditorDockable dockable, EditorLayout layout ){
			return false;
		}

		@Override
		public EditorDockable read( EditorLayout layout ){
			return new EditorDockable( this, layout );
		}

		@Override
		public EditorLayout write( EditorDockable dockable ){
			return dockable.getLayout();
		}
	}

	public static class EditorLayout implements MultipleCDockableLayout{
		@Override
		public void readStream( DataInputStream in ) throws IOException{
			// ignore
		}

		@Override
		public void readXML( XElement element ){
			// ignore
		}

		@Override
		public void writeStream( DataOutputStream out ) throws IOException{
			// ignore	
		}

		@Override
		public void writeXML( XElement element ){
			// ignore
		}

	}

	public static class EditorDockable extends DefaultMultipleCDockable{
		private EditorLayout itsLayout=null;

		public EditorDockable( EditorFactory factory, EditorLayout layout ){
			/* it is mandatory to set the factory, the EditorDockable cannot be created
			 * without it. */
			super( factory );
			this.itsLayout=layout;

			//	           
			//	            /* and then we just set up the editor */
			//	            setTitleText( layout.getFileName() );
			//	           
			//	            if(layout.getFileContent()!=null){
			//	                for(ImageComponent e: layout.getFileContent()){
			//	                    e.setParentDockable(this);
			//	                    add(e);
			//	                }
			//	            }
			//	            add(layout.getBackground());

		}


		/* This convenient method allows us to grab the entire content of this dockable
		 * in one step. */
		public EditorLayout getLayout(){
			return itsLayout;
		}
	}

	public static class DropTargetDockable extends DropTarget {

		private EditorDockable container;

		public DropTargetDockable(EditorDockable container, int ops, DropTargetListener dtl ){
			setComponent( container.getContentPane() );
			this.container=container;
			setDefaultActions(ops);
			try {
				addDropTargetListener( dtl);
			} catch (TooManyListenersException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public EditorDockable getDockable(){
			return this.container;
		}
	}

	public static class DragDrop implements DragGestureListener, DropTargetListener {
		private boolean dragging = false;
		
		public DragDrop( CControl control ){
			// maybe there is a way such that the relocator does not need to be canceled. I'll
			// explore this option for the next release. In the mean time this piece of code remains
			// necessary.
			control.getController().getRelocator().addVetoableDockRelocatorListener( new VetoableDockRelocatorListener(){				
				@Override
				public void searched( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void grabbing( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void grabbed( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void dropping( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void dropped( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void dragging( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void dragged( DockRelocatorEvent event ){
					check( event );
				}
				
				@Override
				public void canceled( DockRelocatorEvent event ){
					check( event );
				}
				
				public void check( DockRelocatorEvent event ){
					if( dragging ){
						event.cancel();
					}
				}
			});
		}
		
		
		
		// DragGestureListener method.
		public void dragGestureRecognized(DragGestureEvent ev) {
			System.out.println("start dnd "+ev.getSource());
			Container contentPane = (Container)ev.getComponent();
			for(int i = 0, n = contentPane.getComponentCount(); i<n; i++){
				Component child = contentPane.getComponent( i );
				if( child instanceof Transferable ){
					ev.startDrag( null, (Transferable)child );
					dragging = true;
					return;
				}
			}
		}

		// DropTargetListener methods.
		public void dragEnter(DropTargetDragEvent ev) {
		}
		public void dragExit(DropTargetEvent ev) {
		}
		public void dragOver(DropTargetDragEvent ev) {
			dropTargetDrag(ev);
		}
		public void dropActionChanged(DropTargetDragEvent ev) {
			dropTargetDrag(ev);
		}
		void dropTargetDrag(DropTargetDragEvent ev) {
			ev.acceptDrag(ev.getDropAction());
		}
		public void drop(DropTargetDropEvent ev) {
			dragging = false;
			
			ev.acceptDrop(ev.getDropAction());
			try {
				Object target = ev.getSource();
				ImageComponent imgComponent = (ImageComponent)ev.getTransferable().getTransferData(new DataFlavor( DataFlavor.javaJVMLocalObjectMimeType ));
				//ImageComponet
				
				final EditorDockable oldEditor = imgComponent.getParentDockable();
				final EditorDockable newEditor = ((DropTargetDockable)target).getDockable();

				newEditor.add(imgComponent);
				imgComponent.setParentDockable( newEditor );
				
				oldEditor.getContentPane().repaint();
				newEditor.getContentPane().repaint();
				
				newEditor.toFront();

			}
			catch (Exception ex) {
				ex.printStackTrace();
			}
			ev.dropComplete(true);
		}
	}
}

1 THANK YOU 4 your help ! hm I changed yout test example with my classes and now it makes the same problems like my application there are classes that i use : i think the problem is in the EditorLayout or im trying to do something wrong with EditorDockable i dont know …

your main that i have changed :

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TooManyListenersException;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.DefaultMultipleCDockable;
import bibliothek.gui.dock.common.MultipleCDockableFactory;
import bibliothek.gui.dock.common.MultipleCDockableLayout;
import bibliothek.gui.dock.control.relocator.DockRelocatorEvent;
import bibliothek.gui.dock.control.relocator.VetoableDockRelocatorListener;
import bibliothek.util.xml.XElement;
 
public class DragAndDropTest {
	private static Image img=ImageHelper.loadImg("Blue-Display.png");
    public static void main( String[] args ){
        JFrame frame = new JFrame( "Test" );
        CControl control = new CControl( frame );
        frame.add( control.getContentArea() );
 
        EditorFactory factory = new EditorFactory();
        control.addMultipleDockableFactory( "editor", factory );
        
        ArrayList <ImageComponent>pc =new ArrayList <ImageComponent>();
        pc.add(new  ImageComponent(null,img,null));
        EditorDockable left= new EditorDockable( factory, new EditorLayout( "left" ,  pc ,new RaumPanel()  )  );
        EditorDockable right= new EditorDockable( factory, new EditorLayout( "right" ,  null ,new RaumPanel()  )  );
 
        control.addDockable( left );
        control.addDockable( right );
 
        left.setVisible( true );
        left.setTitleText( "Left" );
 
        right.setLocation( CLocation.base().normalEast( 0.5 ) );
        right.setVisible( true );
        right.setTitleText( "Right" );
        
        DragSource dragSource = new DragSource();
        DragDrop dndListener = new DragDrop() ;
        initDockable( left, dragSource, dndListener );
        initDockable( right, dragSource, dndListener );
 
        frame.setBounds( 20, 20, 500, 400 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
    }
    
    public static void initDockable(EditorDockable editor, DragSource dragSource, DragDrop dndListener){
        DropTargetDockable target = new DropTargetDockable(editor, DnDConstants.ACTION_MOVE, dndListener);
        dragSource.createDefaultDragGestureRecognizer( editor.getContentPane().getComponent(0), DnDConstants.ACTION_MOVE, dndListener );
    }
    
   
 
    
 
    public static class DropTargetDockable extends DropTarget {
 
        private EditorDockable container;
 
        public DropTargetDockable(EditorDockable container, int ops, DropTargetListener dtl ){
            setComponent( container.getContentPane() );
            this.container=container;
            setDefaultActions(ops);
            try {
                addDropTargetListener( dtl);
            } catch (TooManyListenersException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
 
        public EditorDockable getDockable(){
            return this.container;
        }
    }
 
}```

than my EditorDockable

```import javax.swing.JTextArea;

import bibliothek.gui.dock.common.DefaultMultipleCDockable;

 class EditorDockable extends DefaultMultipleCDockable{
		
	 private EditorLayout itsLayout=null;
		
		public EditorDockable( EditorFactory factory, EditorLayout layout ){
			/* it is mandatory to set the factory, the EditorDockable cannot be created 
			 * without it. */
			super( factory );
			this.itsLayout=layout;
			
			
			/* and then we just set up the editor */
			setTitleText( layout.getFileName() );
			
			if(layout.getFileContent()!=null){
				for(ImageComponent e: layout.getFileContent()){
					e.setParentDockable(this);
					add(e);
				}
			}
			add(layout.getBackground());
			
		}
		
	
		/* This convenient method allows us to grab the entire content of this dockable
		 * in one step. */
		public EditorLayout getLayout(){
			
			return itsLayout;
		}
	}```


EditorLayout

```import java.awt.Color;
import java.awt.Cursor;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JPanel;

import bibliothek.gui.dock.common.MultipleCDockableLayout;
import bibliothek.util.xml.XElement;

class EditorLayout implements MultipleCDockableLayout {
		private String fileName=null;
		private JPanel background=null;
		private  ArrayList <ImageComponent>pc;
	
		
		public EditorLayout(){
			// nothing
		}
		
		public EditorLayout( String fileName,  ArrayList <ImageComponent>pc, JPanel background ){
			
			
			System.out.println("newEditorLayout + "+ background +" name "+fileName);
			this.fileName = fileName;
			this.pc=pc;
			this.background=background;
			
		}
		
		public String getFileName(){
			return fileName;
		}
		
		public ArrayList<ImageComponent> getFileContent(){
			return pc;
		}
		
		public JPanel getBackground(){
			return background;
		}
		
		@Override
		public boolean equals( Object obj ){
			if( this == obj ){
				return true;
			}
			if( obj == null ){
				return false;
			}
			if( getClass() != obj.getClass() ){
				return false;
			}
			EditorLayout other = (EditorLayout) obj;
			return equals( background, other.background ) && 
				equals( fileName, other.fileName ); /*&& 
				/*equals( fileContent, other.fileContent );*/
		}
		
		private boolean equals( Object a, Object b ){
			if( a == null ){
				return b == null;
			}
			else{
				return a.equals( b );
			}
		}

		public void readStream( DataInputStream in ) throws IOException{
			fileName = in.readUTF();
			
		}

		public void readXML( XElement element ){
			fileName = element.getElement( "name" ).getString();
			
		}

		public void writeStream( DataOutputStream out ) throws IOException{
			out.writeUTF( fileName );
			
		}

		public void writeXML( XElement element ){
			element.addElement( "name" ).setString( fileName );
			
		}
		
		public EditorLayout clone(){
			System.out.println("clone layout");
			return this;
		}

		
	
		
		
	}

factor




/* This factory builds a link between EditorDockable and EditorLayout */

 class EditorFactory implements MultipleCDockableFactory<EditorDockable, EditorLayout>{
		/* An empty layout is required to read a layout from an XML file or from a byte stream */
		public EditorLayout create(){
			return new EditorLayout();
		}

		/* An optional method allowing to reuse 'dockable' when loading a new layout */
		public boolean match( EditorDockable dockable, EditorLayout layout ){
			System.out.println("factory");
			return dockable.getLayout().equals( layout );
		}

		/* Called when applying a stored layout */
		public EditorDockable read( EditorLayout layout ){
			System.out.println("factory");
			return new EditorDockable( this, layout );
		}

		/* Called when storing the current layout */
		public EditorLayout write( EditorDockable dockable ){
			System.out.println("factory");
			return dockable.getLayout();
		}
		
	}```

ImageComponent class: 

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;

import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ImageComponent extends Component {

  private boolean dragging=false;
  private Image image;
  private Point offset;
  private DataComputer itsPC=null;
  public DataComputer getItsPC() {
	return itsPC;
}


public void setItsPC(DataComputer itsPC) {
	this.itsPC = itsPC;
}


private Control dataControl=null;
  private EditorDockable parentDockable=null;
	
public ImageComponent(DataComputer PC, Image img, final Control dataControl){
	
	this.dataControl=dataControl;
	this.itsPC=PC;
	//this.setBounds((int)PC.getItsPosition().getX() , (int)PC.getItsPosition().getY(), 50, 50); 
	this.setBounds(50,50,50,50);
	this.image=img;
	
	
	setVisible(true);
	
      
      this.addMouseMotionListener(new MouseMotionListener(){

  		@Override
  		public void mouseDragged(MouseEvent e) {
  			/* if(dragging)  
             {  
  				 
                 Rectangle r = getBounds();
                 r.x += e.getX() - offset.x;  
                 r.y += e.getY() - offset.y;  
                setBounds(r);
                
             }  
  			 */
  		
  		}

  		
  		public void mouseMoved(MouseEvent e) {
  			
  			
  			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  			
  		}
  		
  	});
      
      this.addMouseListener(new MouseListener(){

		
		public void mouseClicked(MouseEvent e) {
			//beim Doppelclick ?ffne die PC Eigenschaften
			if(e.getClickCount()==2 ){
				
				try {
					dataControl.showProperties(ImageComponent.this.itsPC);
				} catch (CloneNotSupportedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			   
	
			}
				
		}
			
		

		
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
		public void mousePressed(MouseEvent e) {
			
			
	         offset = e.getPoint();  
	            dragging = true;  
			
			
		}

		public void mouseReleased(MouseEvent arg0) {
			 dragging = false;  
			 //ImageComponent.this.itsPC.setItsPosition(ImageComponent.this.getLocation());
			
		}
    	
    });
     
      
	
}

public void paint (Graphics g) {

  super.paint(g);
  
 // g.drawImage(image, 0,0, 50,50,null);
  
  g.setColor( Color.RED );
  g.fillOval( 0, 0, 20, 20 );
 	        	
        
        	
  }

public void updateComponent(){

  this.setBounds((int)this.itsPC.getItsPosition().getX() , (int)this.itsPC.getItsPosition().getY(), 50, 50); 
  switch(itsPC.getItsStauts()){
  
  case NEU: image=ImageHelper.loadImg("Blue-Display.png");
  	break;
  case INTAKT: image=ImageHelper.loadImg("Green-Display.png");
	break;
  case DEFECT: image=ImageHelper.loadImg("Red-Display.png");
	break;
  case GEMELDET: image=ImageHelper.loadImg("Yellow-Display.png");
	break;
  
  }

}

public EditorDockable getParentDockable() {
return parentDockable;
}

public void setParentDockable(EditorDockable parentDockable) {
this.parentDockable = parentDockable;

}

}```

DnD class:

import java.awt.datatransfer.*;
import java.awt.dnd.*;

import javax.swing.JButton;
public class DragDrop implements DragGestureListener, 
DragSourceListener,
        DropTargetListener, Transferable {
    static final DataFlavor[] supportedFlavors = {null};
    
    static {
        try {
            supportedFlavors[0] = new 
            DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    Object object;
    // Transferable methods.
    public Object getTransferData(DataFlavor flavor) {
        if (flavor.isMimeTypeEqual
            (DataFlavor.javaJVMLocalObjectMimeType)) {
            return object;
        } else {
        	
            return null;
        }
    }
    public DataFlavor[] getTransferDataFlavors() {
        return supportedFlavors;
    }
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.isMimeTypeEqual
        (DataFlavor.javaJVMLocalObjectMimeType);
    }
    // DragGestureListener method.
    public void dragGestureRecognized(DragGestureEvent ev) {
    	System.out.println("start dnd "+ev.getSource());
    	   ev.startDrag(null, this, this);
    }
    // DragSourceListener methods.
    public void dragDropEnd(DragSourceDropEvent ev) {
    }
    public void dragEnter(DragSourceDragEvent ev) {
    }
    public void dragExit(DragSourceEvent ev) {
    }
    public void dragOver(DragSourceDragEvent ev) {
    	
        object = ev.getSource();
    }
    public void dropActionChanged(DragSourceDragEvent ev) {
    }
    // DropTargetListener methods.
    public void dragEnter(DropTargetDragEvent ev) {
    }
    public void dragExit(DropTargetEvent ev) {
    }
    public void dragOver(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    public void dropActionChanged(DropTargetDragEvent ev) {
        dropTargetDrag(ev);
    }
    void dropTargetDrag(DropTargetDragEvent ev) {
        ev.acceptDrag(ev.getDropAction());
    }
    public void drop(DropTargetDropEvent ev) {
        ev.acceptDrop(ev.getDropAction());
        try {
            Object target = ev.getSource();
            Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);
            //ImageComponet
            Component component = ((DragSourceContext)source).getComponent();
            ImageComponent  imgComponent=(ImageComponent)component;
            EditorDockable oldEditor = imgComponent.getParentDockable();
            
            Container container = (Container) ((DropTarget) 
                    target).getComponent();
           
           
           
          
           container.add(imgComponent);
           oldEditor.getContentPane().validate();
           oldEditor.getContentPane().repaint();
           container.validate();
           container.repaint();
        
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        ev.dropComplete(true);
    }
    
}```

hm fond the problem but not the solution

ad to your example this

left.getContentPane().add( new RaumPanel());
right.getContentPane().add( new ImageComponent( right ) );
right.setTitleText( “Right” );
and paint the circle smaller like that

g.fillOval( 0, 0, 30, 30 );

u will see the problem !

hm

any ideas ? need to fix it as quickly as possible !
i tried to create a new dockableEditor and to put the panel and components on it but with no success :frowning:

I see that one panel is not shown. But that is an issue with the BorderLayout. Setting another LayoutManager and both panels show up (in this case one at the top, one at the bottom):

		right.getContentPane().add(new RaumPanel());
		right.getContentPane().add(new ImageComponent(right));```

I'm a bit confused, but by now I'm pretty sure that your problem has nothing to do with DockingFrames at all. If you would take some JFrames instead of your EditorDockable you would probably have the same issues.

my idea is to to get data from old components and create new components than save dockable position
delete old dockable crate a new one put components on it , put dockable on the old position :slight_smile:
„pseudo“ DnD :slight_smile: but if u have a better one write pliz because im not sure if it’ll work

[QUOTE=Beni]I see that one panel is not shown. But that is an issue with the BorderLayout. Setting another LayoutManager and both panels show up (in this case one at the top, one at the bottom):

		right.getContentPane().add(new RaumPanel());
		right.getContentPane().add(new ImageComponent(right));```

I'm a bit confused, but by now I'm pretty sure that your problem has nothing to do with DockingFrames at all. If you would take some JFrames instead of your EditorDockable you would probably have the same issues.[/QUOTE]

i have to move the components on the dockable/Frame  and  need  DnD  to move it to another frame but its not the problem in my application the panel is shown up and i can move the components. 

  yeah may be but why :) ?

It feels to me as if there is a wild mix of data to show and user interface that paints data. A clear separation of these two things - model and view - might make things a lot easier. I think writing a model that fully describes the data the user is editing, but does not know anything about Components or Dockables would be a good idea.

Then I think you should not move around entire Components, but the data they represent. Deleting and creating new Dockables due to a DnD operation is just overkill, and unnecessarily complex. I would design one Component that shows a „room“ and its „images“. And I would make this Component intelligent enough to handle rooms and images that change. DnD would just be used to change rooms (e.g. add/remove images), but has no additional effects like moving Components.

I don’t know how exactly these rooms and images are linked, I’m just doing some wild guessing here :slight_smile:

[QUOTE=Beni]It feels to me as if there is a wild mix of data to show and user interface that paints data. A clear separation of these two things - model and view - might make things a lot easier. I think writing a model that fully describes the data the user is editing, but does not know anything about Components or Dockables would be a good idea.

Then I think you should not move around entire Components, but the data they represent. Deleting and creating new Dockables due to a DnD operation is just overkill, and unnecessarily complex. I would design one Component that shows a „room“ and its „images“. And I would make this Component intelligent enough to handle rooms and images that change. DnD would just be used to change rooms (e.g. add/remove images), but has no additional effects like moving Components.

I don’t know how exactly these rooms and images are linked, I’m just doing some wild guessing here :-)[/QUOTE]

forgive me James Gosling but i gonna make the overkill :slight_smile:

lol

But I admit I’m a bit out of ideas. Would it be possible that you send me your entire application in a zip file?

I had a dream ! , I had a dream tonight and i spoke to morgan freeman he said me that u a right and i dont need to make the overkill : there is the solution :

        ev.acceptDrop(ev.getDropAction());
        try {
            Object target = ev.getSource();
            Object source = ev.getTransferable().getTransferData(supportedFlavors[0]);
            ImageComponent component = (ImageComponent) ((DragSourceContext) 
                    source).getComponent();
            
            EditorDockable newEditor=(EditorDockable) ((DropTargetDockable) target).getDockable();
            EditorDockable oldEditor=component.getParentDockable();
            RaumPanel backgroundPanel=null;
            
            //find BackgroundPanel -> RaumPanel this panel mus be the lats one i dont know why :)
           for(int i=0;i<newEditor.getContentPane().getComponentCount();i++){
        	   
        	   if(newEditor.getContentPane().getComponent(i) instanceof RaumPanel){
        		   //Save it
        		   backgroundPanel=(RaumPanel) newEditor.getContentPane().getComponent(i);
        		   //delete from  dockable
        		   newEditor.remove(newEditor.getContentPane().getComponent(i));
        	   }
        		   
           }
           
            
          
           //very important this shit i dont know why :)
           newEditor.setLayout(null);
         //Add the dragged  ImageComponent to the Dockabel
            newEditor.add(component);
            component.setParentDockable(newEditor);
            // in the End add background
            newEditor.add(backgroundPanel);
           
            //repaint all 
            component.repaint();
            
            oldEditor.setVisible(true);
            oldEditor.getContentPane().validate();
            oldEditor.getContentPane().repaint();
            
            newEditor.setVisible(true);
            newEditor.getContentPane().validate();
            newEditor.getContentPane().repaint();
            
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        ev.dropComplete(true);
    }```

now i can drag and drop but i have 2 problem 

1 if i resize the dockable my background panel doesnt resize to my be beause of layout null hm how cann i add a resize Listener to the EditorDockable whiich is extended from DefaultMultipleCDockable so i could every time resize the background bit setSize(dockableWidth,DockableHeight);

2) if i move the component  the DnD starts aoutomaticly but i dont want this ! i want to start DnD first if the mouse is above the current Dockable , if not i use the mouseListener and repaintthe Imagecomponent on the dockabel
  1. Add a ComponentListener to the contentPane of the Dockable. It will be called everytime the size changes. Or write your custom LayoutManager, it’s a surprisingly powerful alternative.

  2. You call “ev.startDrag(null, this, this);” in the “dragGestureRecognized” method. Can’t you just delay this “startDrag”? (I’ve not tested that).