How to Drag and Drop a Button from One Panel to Another Panel

How to Drag and Drop(copy) from one panel to another panel

Did you try something like this ?

private void Form5_Load(object sender, EventArgs e)  
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}

void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}

void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(c);
}
}

void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

VB.NET

Private Sub Form5_Load(sender As Object, e As EventArgs)
Me.panel1.AllowDrop = True
For Each c As Control In Me.panel1.Controls
c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
Next
Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub

Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
Dim c As Control = TryCast(sender, Control)
c.DoDragDrop(c, DragDropEffects.Move)
End Sub

Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
If c IsNot Nothing Then
c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
Me.panel1.Controls.Add(c)
End If
End Sub

Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub

Source

How to drag and drop a button from one panel to another panel?

For demonstration, place two panels on a form and a button in one of the panels:

public Form1() {
InitializeComponent();

panel1.AllowDrop = true;
panel2.AllowDrop = true;

panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;

panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;

button1.MouseDown += button1_MouseDown;
}

void button1_MouseDown(object sender, MouseEventArgs e) {
button1.DoDragDrop(button1, DragDropEffects.Move);
}

void panel_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}

void panel_DragDrop(object sender, DragEventArgs e) {
((Button)e.Data.GetData(typeof(Button))).Parent = (Panel)sender;
}

JavaFX drag button from one pane to another

There's no way to add a button to the clipboard content (i.e. to the dragboard). You can only add specific types (string, image) and objects that implement serializable (button doesn't, and it wouldn't do what you wanted anyway). The drag-and-drop API is very deficient in this aspect, imho. You should just add some dummy text to the dragboard and keep a reference to the button that is currently being dragged.

Quick SSCCE:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class DragAndDropButton extends Application {

private final DataFormat buttonFormat = new DataFormat("com.example.myapp.formats.button");

private Button draggingButton ;

@Override
public void start(Stage primaryStage) {
FlowPane pane1 = new FlowPane();
FlowPane pane2 = new FlowPane();

for (int i = 1 ; i <= 10; i++) {
pane1.getChildren().add(createButton("Button "+i));
}

addDropHandling(pane1);
addDropHandling(pane2);

SplitPane splitPane = new SplitPane(pane1, pane2);
splitPane.setOrientation(Orientation.VERTICAL);

Scene scene = new Scene(splitPane, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

private Button createButton(String text) {
Button button = new Button(text);
button.setOnDragDetected(e -> {
Dragboard db = button.startDragAndDrop(TransferMode.MOVE);
db.setDragView(button.snapshot(null, null));
ClipboardContent cc = new ClipboardContent();
cc.put(buttonFormat, "button");
db.setContent(cc);
draggingButton = button ;
});
button.setOnDragDone(e -> draggingButton = null);
return button ;
}

private void addDropHandling(Pane pane) {
pane.setOnDragOver(e -> {
Dragboard db = e.getDragboard();
if (db.hasContent(buttonFormat)
&& draggingButton != null
&& draggingButton.getParent() != pane) {
e.acceptTransferModes(TransferMode.MOVE);
}
});

pane.setOnDragDropped(e -> {
Dragboard db = e.getDragboard();
if (db.hasContent(buttonFormat)) {
((Pane)draggingButton.getParent()).getChildren().remove(draggingButton);
pane.getChildren().add(draggingButton);
e.setDropCompleted(true);
}
});
}

public static void main(String[] args) {
launch(args);
}
}

How do I drag and drop copy of controls

I got my own solution.

First of all set the AllowDrop property of Panel to true.

panel1.AllowDrop=true;

Create a DragEnter event for Panel from properties window

private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

Next, create a DragDrop event for Panel from properties window

private void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Declare rnd globally for creating random id for dynamic button(eg : Random rnd = new Random();)
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
btn.Click += new System.EventHandler(DynamicButton_Click);
if (c != null)
{
btn.Text = c.Text;
btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(btn);
}
}

Drag and Drop between two panels using swing?

This is not how drag and drop is accomplished in Java/Swing

There are plenty of examples on SO...

  • Drag and Drop custom object from JList into JLabel
  • Java - How to drag and drop JPanel with its components
  • how to drag and drop files from a directory in java
  • Java - visually drag a swing element

I'd also recommend that you checkout How to drag and drop with Java 2

Drag and Drop moving controls instead of copying

If you want to copy the control, then you'll need to actually take a copy of the control being copied. The best place to do this is probably in the place where you change the mode from move to copy. You can either leave the copy in the original location and continue to move the original or move the copy.

So where you have:

Button data = (Button)e.Data.GetData(typeof(Button));

you'll need to either clone the button or create a new Button and set the properties manually. Cloning would be the better solution.

Implementing drag-and-drop from one JPanel to another

May be this can help you.

Drag and Drop of complex custom objects in Java

Move controls when Drag and drop on panel in C#

If your control is already on the panel and you're simply moving it within the same panel, then using the Mouse events is probably the easiest way to do this. My understanding is that Drag and Drop is more about conveying data between controls or even applications. Drag and drop would be a good fit if you're trying to allow a control to transfer between panels, for example.


If you want to do both, then here's one possible idea:

  1. Perform move dragging within the same panel using your Mouse events.

  2. When you get a MouseLeave event on the panel, begin a DragDrop operation (some examples here) You can either remove the control from the panel or add some sort of 'gray out' effect to indicate that the control may be leaving.

  3. Handle the DragDrop on your target panel and place the control at the mouse location of the drop.

This combines the intuitive feel of dragging the control around, while also providing a way to drag 'past' the panel and on to a new surface.



Related Topics



Leave a reply



Submit