我有一个Tilepane(源码),它载有我存储在数据库中的图像, 我想拖动我的Tilepane中加载的任何图像并将其拖放到我在FXML场景构建器中提供的某个Imageview(目标)。
我在startDragAndDrop(TransferMode.MOVE)中遇到了麻烦,因为我的源中的图像不是最终的imageview,因为它们是从数据库加载的。
我需要一个如何处理从我的Tilepane(源)中将图像放入JavaFX ImageView(目标)的示例。
感谢和问候, 步骤。
try {
Try2.pstmtGetAlbumPhoto = Try2.getConnection().prepareStatement(RetrievePhoto);
ResultSet RSphoto = Try2.pstmtGetAlbumPhoto.executeQuery();
while(RSphoto.next()){
imageView = new ImageView();
imageView.setFitWidth(120);
imageView.setFitHeight(70);
String Path = RSphoto.getString("Path");
System.out.println(Path);
BufferedImage images = null;
try {
images = ImageIO.read(new File(Path));
} catch (IOException ex) {
System.out.println("Image failed to load.");
}
WritableImage wr = null;
if (images != null) {
wr = new WritableImage(images.getWidth(), images.getHeight());
PixelWriter pw = wr.getPixelWriter();
for (int x = 0; x < images.getWidth(); x++)
{
for (int y = 0; y < images.getHeight(); y++) {
pw.setArgb(x, y, images.getRGB(x, y));
}
}
}
imageView.setImage(wr);
final ImageView currentImage = new ImageView();
currentImage.setImage(wr);
currentImage.setId(this.getClass().getSimpleName() + System.currentTimeMillis());
imageView.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard db = currentImage.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putImage(currentImage.getImage());
content.putString(getName());
db.setContent(content);
event.consume();
}
});
target.setOnDragOver(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.hasImage() || db.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
}
}
});
target.setOnDragDropped(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.hasImage()) {
target.setImage(db.getImage());
String x = db.getString();
System.out.println(x);
;
event.setDropCompleted(true);
}
}
});
TilePane3.getChildren().addAll(imageView);
}
} catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}