drag and drop a object in swt

Status
Nicht offen für weitere Antworten.

matthias333

Mitglied
gibts eine möglichkeit per drag and drop eigene object zu kopieren?

das ganze natürlich in swt... :roll:

mfg matthias
 

matthias333

Mitglied
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);
			}
		});
	}
 

matthias333

Mitglied
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.
Ähnliche Java Themen
  Titel Forum Antworten Datum
A Swing Drag and Drop TreeNode User Object AWT, Swing, JavaFX & SWT 3
J Nochmal Drag&Drop, ABER: Object verschieben AWT, Swing, JavaFX & SWT 2
A JavaFX JavaFX images per Drag&Drop verschieben AWT, Swing, JavaFX & SWT 1
J Drag und drop aus einer JTable - bitte um Unterstützung AWT, Swing, JavaFX & SWT 2
G JPanel per Drag and Drop JButtons und Bilder ablegen AWT, Swing, JavaFX & SWT 1
AmsananKING ListView Drag And Drop AWT, Swing, JavaFX & SWT 0
AmsananKING Drag And Drop Filenames Inside A Listview AWT, Swing, JavaFX & SWT 1
DonBronson Java Graphics bewegbar machen (Drag&Drop) AWT, Swing, JavaFX & SWT 3
M Polygon per Drag&Drop verschieben AWT, Swing, JavaFX & SWT 26
Z Swing Drag and Drop mit einem JButton AWT, Swing, JavaFX & SWT 1
N Drag and Drop Fenster AWT, Swing, JavaFX & SWT 11
F Drag&Drop mit Transparenter Farbe bei PNG AWT, Swing, JavaFX & SWT 0
D JavaFX Pane per Drag&Drop bewegen? AWT, Swing, JavaFX & SWT 2
L JavaFX Drag and Drop funktioniert nicht AWT, Swing, JavaFX & SWT 3
J Drag and Drop von eigenen Objekten AWT, Swing, JavaFX & SWT 3
J Drag and Drop eines Buttons AWT, Swing, JavaFX & SWT 0
T Swing Drag and Drop für JComponents AWT, Swing, JavaFX & SWT 1
Z Swing Drag&Drop zwischen JTable und JTree AWT, Swing, JavaFX & SWT 4
F Drag und Drop AWT, Swing, JavaFX & SWT 0
L JavaFX JavaFX Chart Drag and Drop AWT, Swing, JavaFX & SWT 3
D JavaFX Drag&Drop mehrerer TreeViews oder TableViews AWT, Swing, JavaFX & SWT 1
P Drag & Drop zwischen Panels AWT, Swing, JavaFX & SWT 0
U Drag and Drop mit imageview AWT, Swing, JavaFX & SWT 0
U Drag and Drop imageviews AWT, Swing, JavaFX & SWT 8
D SteelSeries in Netbeans als Drag-and-Drop einbinden AWT, Swing, JavaFX & SWT 0
C JTable Drag and Drop von Zeilen innerhalb einer Table AWT, Swing, JavaFX & SWT 2
S Swing Update eine JTabelle nach einer Drag&Drop Operation AWT, Swing, JavaFX & SWT 0
S Swing Suche Drag & Drop Beispiele AWT, Swing, JavaFX & SWT 1
A Drag and Drop mit JAVAFX- Scenebuilder AWT, Swing, JavaFX & SWT 1
R Performance Drag and Drop & Timer AWT, Swing, JavaFX & SWT 3
R Drag and Drop Problem auf Jpanel AWT, Swing, JavaFX & SWT 2
N Swing JTable und Drag und Drop AWT, Swing, JavaFX & SWT 2
A Drag and Drop eigener Objekte AWT, Swing, JavaFX & SWT 7
C Drag and Drop (inventar) AWT, Swing, JavaFX & SWT 15
F Swing Drag and Drop in JTree aus verschiedenen Listen AWT, Swing, JavaFX & SWT 6
T Swing JButton per Drag&Drop verschieben AWT, Swing, JavaFX & SWT 5
Iron Monkey JFileChooser - Drag and Drop AWT, Swing, JavaFX & SWT 5
Iron Monkey Nach Drag & Drop die Datei auf Komponent darstellen AWT, Swing, JavaFX & SWT 2
M AWT Drag n Drop-Support für Component AWT, Swing, JavaFX & SWT 5
HaukeG Swing Drag & Drop in verschiedenen Varianten AWT, Swing, JavaFX & SWT 4
S Swing Drag&Drop mit TransferHandler und JPanels AWT, Swing, JavaFX & SWT 8
C Swing Simulation von Drag and Drop Events AWT, Swing, JavaFX & SWT 3
H Swing "Drag and Drop" eines JComponent über ein JPanel AWT, Swing, JavaFX & SWT 2
R Drag 'n Drop AWT, Swing, JavaFX & SWT 3
S Selektion bei Drag&Drop AWT, Swing, JavaFX & SWT 4
C Swing Drag and Drop mit Objekten in einem Fenster. AWT, Swing, JavaFX & SWT 9
T SWT Drag&Drop: Eclipse FileTransfer mit Icons AWT, Swing, JavaFX & SWT 14
F Drag & Drop durch Verbindungslinien AWT, Swing, JavaFX & SWT 10
T Swing Drag and Drop - JLabels tauschen statt überschreiben AWT, Swing, JavaFX & SWT 11
S Drag and Drop über 2 Panels AWT, Swing, JavaFX & SWT 2
K JButtons innerhalb eines JPanels verschieben (DRAG&DROP) AWT, Swing, JavaFX & SWT 5
B Drag and Drop AWT, Swing, JavaFX & SWT 6
K Drag and Drop Workbench AWT, Swing, JavaFX & SWT 2
P SWT Eclipse Draw2D Drag and Drop (ruckelt) AWT, Swing, JavaFX & SWT 4
F SWT Drag and Drop im TreeViewer AWT, Swing, JavaFX & SWT 4
B Swing Drag&Drop mit Feedback (Image am Mauszeiger) AWT, Swing, JavaFX & SWT 7
Spin JFrame/ Frame Drag and Drop AWT, Swing, JavaFX & SWT 13
A TransferHandler & Drag n' Drop AWT, Swing, JavaFX & SWT 2
R Drag an Drop JTable Zelle AWT, Swing, JavaFX & SWT 6
D Drag & Drop - node.isRoot AWT, Swing, JavaFX & SWT 3
E Swing Drag n Drop Verschieben von Labels o.ä. AWT, Swing, JavaFX & SWT 10
E Swing Beim Drag & Drop, Drag verbieten?! AWT, Swing, JavaFX & SWT 2
E JTree Autoscroll bei Drag and Drop AWT, Swing, JavaFX & SWT 4
F Swing Problem mit Drag&Drop in JTable AWT, Swing, JavaFX & SWT 4
C keine weiteren Events während Drag&Drop Operation möglich? AWT, Swing, JavaFX & SWT 5
E Drag&Drop zwischen 2 Listen AWT, Swing, JavaFX & SWT 5
0 Swing Drag n' Drop Bug wenn Source und Target gleiche Komponente? AWT, Swing, JavaFX & SWT 4
C Drag and Drop JPanel auf JPanel nach drop erneut verschieben? AWT, Swing, JavaFX & SWT 3
M Swing JTable Drag'n'Drop von Dateien AWT, Swing, JavaFX & SWT 3
M Drag and Drop: Quellfenster AWT, Swing, JavaFX & SWT 2
M Buttons per Drag & Drop im GridBagLayout verschieben AWT, Swing, JavaFX & SWT 6
M Swing JList > Drag & Drop AWT, Swing, JavaFX & SWT 2
C Drag an Drop vom JTree zur JTable AWT, Swing, JavaFX & SWT 4
Z Drag and Drop auf Application AWT, Swing, JavaFX & SWT 3
G Drag and Drop JTree to Canvas AWT, Swing, JavaFX & SWT 7
H Drag&Drop von JComponents AWT, Swing, JavaFX & SWT 6
G JTable drag and drop AWT, Swing, JavaFX & SWT 4
H Drag&Drop mit GWT AWT, Swing, JavaFX & SWT 8
B Swing Drag&Drop einzelner Zellen in einer JTable AWT, Swing, JavaFX & SWT 12
P JList: Reihenfolge der Elemente per Drag'n'Drop ändern. AWT, Swing, JavaFX & SWT 9
K Swing Wie ändere ich die default action für Drag&Drop AWT, Swing, JavaFX & SWT 6
R JLayeredPane - Drag&Drop - mouseDragged AWT, Swing, JavaFX & SWT 6
C JTable mit RowSorter und Drag & Drop: Zeile verschieben AWT, Swing, JavaFX & SWT 4
V SWT TreeViewer Drag'n'Drop LocalSelectionTransfer AWT, Swing, JavaFX & SWT 10
R Swing JLayeredPane - Drag&Drop Positionen vertauschen AWT, Swing, JavaFX & SWT 3
F Drag & Drop mit eigenen Komponenten AWT, Swing, JavaFX & SWT 2
B SWT - Drag & Drop innerhalb einer Table AWT, Swing, JavaFX & SWT 3
S Drag'n'Drop AWT, Swing, JavaFX & SWT 8
E Drag&Drop JTable; Renderer füllt alle Zellen AWT, Swing, JavaFX & SWT 10
M Drag & Drop in Swing (createTransferable) AWT, Swing, JavaFX & SWT 6
T Drag Quelle beim Drop AWT, Swing, JavaFX & SWT 6
A Drag & Drop von Zeilen innerhalb einer Tabelle AWT, Swing, JavaFX & SWT 2
E Drag & Drop von jTree in JList AWT, Swing, JavaFX & SWT 5
P Dateien per Drag&Drop ins Java-Fenster ziehen AWT, Swing, JavaFX & SWT 8
G JTree Node ggf. aufklappen bei Drag & Drop? AWT, Swing, JavaFX & SWT 7
J Drag'n Drop imm selben Frame unterbinden AWT, Swing, JavaFX & SWT 3
S Table Row per Drag and Drop in andere Table schieben? AWT, Swing, JavaFX & SWT 14
X wiedermal Drag n Drop AWT, Swing, JavaFX & SWT 2
P Drag & Drop AWT, Swing, JavaFX & SWT 2
X Drag and Drop AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen


Oben