我正在尝试将自定义对象从JPanel拖放到JavaFX场景中
为此,我使用自定义对象和自定义TransferHandler创建了一个简单的应用程序。我在这种情况下的问题是,当我在dragboard = event.getDragboard();
或onDragOver()
方法中设置onDragDropped()
的断点时,JavaFX dragboard只有内容。在断点处停止后,我可以删除断点,DnD每次都在工作。
任何人都可以帮助我让事情没有断点吗?
提前谢谢你。
这是我的代码:
MyObject
public class MyObject
{
private final String text;
public MyObject(String text)
{
this.text = text;
}
public String getText()
{
return text;
}
}
MyObjectTransferable
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class MyObjectTransferable implements Transferable
{
public static final DataFlavor DATAFLAVOR_MYOBJECT = new DataFlavor(MyObject.class, "myObject");
private final MyObject obj;
public MyObjectTransferable(MyObject obj)
{
this.obj = obj;
}
@Override
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[] {DATAFLAVOR_MYOBJECT};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return DATAFLAVOR_MYOBJECT.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
return DATAFLAVOR_MYOBJECT.equals(flavor) ? obj : null;
}
}
SwingDnDtoFX
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.GridPane;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SwingDnDtoFX
{
private JLabel statusLabel;
public static void main(String[] args)
{
new SwingDnDtoFX().init();
}
private void init()
{
final JFrame frame = new JFrame();
frame.setSize(1000, 500);
frame.setContentPane(createPanel());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private JPanel createPanel()
{
final JPanel panel;
final JPanel spacePanel;
final Dimension dimension;
panel = new JPanel(new BorderLayout());
spacePanel = new JPanel(new BorderLayout());
dimension = new Dimension(300, 500);
statusLabel = new JLabel();
spacePanel.setBackground(Color.RED);
spacePanel.add(statusLabel, BorderLayout.CENTER);
panel.add(createDragPanel(dimension), BorderLayout.WEST);
panel.add(spacePanel, BorderLayout.CENTER);
panel.add(createDropPanel(dimension), BorderLayout.EAST);
return panel;
}
private JPanel createDragPanel(final Dimension dimension)
{
final JPanel dragPanel;
final JLabel dragLabel;
final DragGestureListener dgl;
final DragSource dragSource;
dragPanel = new JPanel(new GridLayout());
dragLabel = new JLabel("Drag me");
dragSource = new DragSource();
dgl = dge ->
{
final MyObject obj = new MyObject(dragLabel.getText());
final MyObjectTransferable transferable = new MyObjectTransferable(obj);
dge.startDrag(null, transferable);
};
dragSource.createDefaultDragGestureRecognizer(dragLabel, DnDConstants.ACTION_COPY, dgl);
dragLabel.setTransferHandler(new TransferHandler("dragLabel"));
dragLabel.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
final JComponent c = (JComponent) e.getSource();
final TransferHandler th = c.getTransferHandler();
th.exportAsDrag(c, e, TransferHandler.COPY);
}
});
dragPanel.setBackground(Color.CYAN);
dragPanel.setPreferredSize(dimension);
dragPanel.add(dragLabel);
return dragPanel;
}
private JPanel createDropPanel(final Dimension dimension)
{
final JPanel dropPanel;
final JLabel dropLabel;
final DropTargetListener dtl;
dropPanel = new JPanel(new GridLayout(0, 1));
dropLabel = new JLabel("Drop here (Swing)");
dtl = new DropTargetAdapter()
{
@Override
public void drop(DropTargetDropEvent dtde)
{
final Transferable transferable;
final DataFlavor[] dataFlavors;
try
{
transferable = dtde.getTransferable();
dataFlavors = transferable.getTransferDataFlavors();
for (int i = 0; i < dataFlavors.length; i++)
{
if (MyObjectTransferable.DATAFLAVOR_MYOBJECT.equals(dataFlavors[i]))
{
final MyObject data;
dtde.acceptDrop(dtde.getDropAction());
data = (MyObject) transferable.getTransferData(dataFlavors[i]);
showStatus(data, "Swing");
dtde.dropComplete(true);
}
}
}
catch (Exception e)
{
e.printStackTrace();
dtde.rejectDrop();
}
}
};
new DropTarget(dropLabel, dtl);
dropLabel.setOpaque(true);
dropLabel.setBackground(Color.CYAN);
dropPanel.setPreferredSize(dimension);
dropPanel.add(dropLabel);
dropPanel.add(createFX());
return dropPanel;
}
private JFXPanel createFX()
{
final JFXPanel fxPanel;
fxPanel = new JFXPanel();
Platform.runLater(() ->
{
final Scene scene;
final GridPane pane;
final Label label;
final Label title;
pane = new GridPane();
scene = new Scene(pane);
label = new Label();
title = new Label("Drop here (JavaFX)");
pane.add(title, 0, 0);
pane.add(label, 0, 1);
pane.setOnDragOver(event ->
{
final Dragboard dragboard;
final DataFormat dataFormat;
dragboard = event.getDragboard();
dataFormat = DataFormat.lookupMimeType("application/x-java-serialized-object");
if (dragboard.hasContent(dataFormat))
event.acceptTransferModes(TransferMode.COPY);
event.consume();
});
pane.setOnDragDropped(event ->
{
final Dragboard dragboard;
final DataFormat dataFormat;
boolean success = false;
dragboard = event.getDragboard();
dataFormat = DataFormat.lookupMimeType("application/x-java-serialized-object");
if (dragboard.hasContent(dataFormat))
{
final Object content;
final MyObject data;
content = dragboard.getContent(dataFormat);
if (content instanceof MyObject)
{
data = (MyObject) content;
label.setText(data.getText());
SwingUtilities.invokeLater(() -> showStatus(data, "JavaFX"));
success = true;
}
}
event.setDropCompleted(success);
event.consume();
});
fxPanel.setScene(scene);
});
return fxPanel;
}
private void showStatus(MyObject data, String framework)
{
final String date = new SimpleDateFormat("HH:mm:ss").format(new Date());
statusLabel.setText(date + " >> " + framework + " >> " + data.getText());
}
}
答案 0 :(得分:1)
var massEmails = $scope.massAddUsers.split(",");
for(massEmailAddr of massEmails ){
massEmailAddr = massEmailAddr.trim();
if(validateEmail(massEmailAddr)){
message.users.push({email: massEmailAddr});
}
else if (massEmailAddr !== "") {
message.users.push({name: massEmailAdd});
}
}
事件中的循环可以完成工作。
onDragOver