drag and drop a object in swt

Status
Nicht offen für weitere Antworten.
anderes problem:

anfangs hab ich mir eine testgui gebaut wo ich von nem table in ein textfeld droppe. -> funktioniert

danach wollt ich das ganze so bauen, dass ich zwei shells habe und von einer shell (mit table) in die andere shell (mit textfield) drope. -> funktioniert nicht

jetzt stellt sich mir die frage is das überhaupt möglich mit 2 verschiedenen shells?
oder passt irgendwas nicht daran das ich das ganze dnd-konstrukt in eigene funktionen packe? (siehe code)

code vom testbeispiel:
Code:
public class DNDtest {

	private Table table;
	protected Shell shell;

	public static void main(String[] args) {
		try {
			DNDtest window = new DNDtest();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void open() {
		Display display;
		display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	protected void createContents() {
		TableColumn tableColumn;
		shell = new Shell();
		shell.setSize(500, 375);
		shell.setText("SWT Application");

		final Label lblSource = new Label(shell, SWT.NONE);
		lblSource.setText("von hier");
		lblSource.setBounds(110, 105, 120, 30);
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		int operations = DND.DROP_MOVE | DND.DROP_COPY;
		DragSource source = new DragSource (lblSource, operations);
		source.setTransfer(types);

		table = new Table(shell, SWT.BORDER);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(65, 190, 158, 138);
		DragSource source2 = new DragSource (table, operations);
		source2.setTransfer(types);

		tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("New column");
		
		TableItem pickerItem = new TableItem(table, SWT.LEFT);
		pickerItem.setText(0, "hallo");
		
		final Text lblTarget;
		lblTarget = new Text(shell, SWT.NONE);
		lblTarget.setText("nach hier");
		lblTarget.setBounds(369, 223, 120, 30);
		DropTarget target = new DropTarget(lblTarget, operations);
		target.setTransfer(types);
		
		source.addDragListener (new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {
			if (lblSource.getText().length() == 0) {
			event.doit = false;
			}
			};
			public void dragSetData (DragSourceEvent event) {
			if
			(TextTransfer.getInstance().isSupportedType(event.dataType)){
			event.data = lblSource.getText();
			}
			}
			public void dragFinished(DragSourceEvent event) {
			if (event.detail == DND.DROP_MOVE)
			lblSource.setText("");
			}
		});
		source2.addDragListener (new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {}
			public void dragSetData (DragSourceEvent event) {
				System.out.println(table.getItem(table.getSelectionIndex()));
			if
			(TextTransfer.getInstance().isSupportedType(event.dataType)){
			event.data = table.getItem(table.getSelectionIndex()).getText();
			}
			}
			public void dragFinished(DragSourceEvent event) {}
		});
		target.addDropListener (new DropTargetAdapter() {
			public void drop(DropTargetEvent event) {
			if (event.data == null) {
			event.detail = DND.DROP_NONE;
			return;
			}
			lblTarget.setText ((String) event.data);
			}
		});
	}

}

-----------------------------------------------

code von shell1:
Code:
private void drag() {
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		int operations = DND.DROP_COPY;
		DragSource source = new DragSource(table_search, operations);
		source.setTransfer(types);
		source.addDragListener (new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {
				System.out.println(table_search.getItem(table_search.getSelectionIndex()));};
			public void dragSetData (DragSourceEvent event) {
				if(TextTransfer.getInstance().isSupportedType(event.dataType)) {
						event.data = table_search.getItem(table_search.getSelectionIndex()).getText(1);
				}
			}
			public void dragFinished(DragSourceEvent event) {}
		});
	}

code von shell2:
Code:
	private void drop() {
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		int operations = DND.DROP_COPY;
		DropTarget target = new DropTarget(txt_name, operations);
		target.setTransfer(types);
		target.addDropListener (new DropTargetAdapter() {
			public void drop(DropTargetEvent event) {
				if (event.data == null) {
						event.detail = DND.DROP_NONE;
						System.out.println("leer");
						return;
				}
				txt_name.setText((String) event.data);
			}
		});
	}
 
argl hab das beispiel programm nu erweitert mit eigenen funktionen.
habs anschließend zweimal ausgeführt und versucht vom einen ins andere zu droppen und es funkt....

Code:
public class DNDtest {

	private Table table;
	protected Shell shell;
	private Label lblSource;
	private Text lblTarget;
	private TableColumn tableColumn;
	public static void main(String[] args) {
		try {
			DNDtest window = new DNDtest();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void open() {
		Display display;
		display = Display.getDefault();
		createContents();
		drag();
		drop();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	protected void createContents() {
		shell = new Shell();
		shell.setSize(500, 375);
		shell.setText("SWT Application");

		lblSource = new Label(shell, SWT.NONE);
		lblSource.setText("von hier");
		lblSource.setBounds(110, 105, 120, 30);

		table = new Table(shell, SWT.BORDER);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(65, 190, 158, 138);

		tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("New column");
		
		TableItem pickerItem = new TableItem(table, SWT.LEFT);
		pickerItem.setText(0, "hallo");
		
		lblTarget = new Text(shell, SWT.NONE);
		lblTarget.setText("nach hier");
		lblTarget.setBounds(369, 223, 120, 30);
		
	}
	private void drag() {
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		int operations = DND.DROP_MOVE | DND.DROP_COPY;
		DragSource source = new DragSource (lblSource, operations);
		source.setTransfer(types);
		DragSource source2 = new DragSource (table, operations);
		source2.setTransfer(types);
		source.addDragListener (new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {
			if (lblSource.getText().length() == 0) {
			event.doit = false;
			}
			};
			public void dragSetData (DragSourceEvent event) {
			if
			(TextTransfer.getInstance().isSupportedType(event.dataType)){
			event.data = lblSource.getText();
			}
			}
			public void dragFinished(DragSourceEvent event) {
			if (event.detail == DND.DROP_MOVE)
			lblSource.setText("");
			}
		});
		source2.addDragListener (new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {}
			public void dragSetData (DragSourceEvent event) {
				System.out.println(table.getItem(table.getSelectionIndex()));
			if
			(TextTransfer.getInstance().isSupportedType(event.dataType)){
			event.data = table.getItem(table.getSelectionIndex()).getText();
			}
			}
			public void dragFinished(DragSourceEvent event) {}
		});
		
	}
	
	private void drop() {
		Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
		int operations = DND.DROP_MOVE | DND.DROP_COPY;
		DropTarget target = new DropTarget(lblTarget, operations);
		target.setTransfer(types);
		target.addDropListener (new DropTargetAdapter() {
			public void drop(DropTargetEvent event) {
			if (event.data == null) {
			event.detail = DND.DROP_NONE;
			return;
			}
			lblTarget.setText ((String) event.data);
			}
		});
		
	}

und bei meinem eigentlichen programm geht gar nix... *heul*
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben