Ich versuche zu implementieren Drag & Löschen Sie ein Element aus ListView mit JavaFX. Alle Handler funktionieren wie onDragDetected, onDragEnter außer onDragDrop.JavaFX OnDragDrop-Handler funktioniert nicht
Es gibt toolbax.fxml mit listView und es ist in main.fxml enthalten.
Hier sind Abschnitte meines Codes:
toolbax.fxml:
<ListView fx:id="source" layoutX="-9.0" prefHeight="200.0" prefWidth="200.0">
<items>
<FXCollections fx:factory="observableArrayList" >
<HBox onDragDetected="#handleDragDetected" onDragDone="#handleDragDone" >
<children>
<ImageView>
<image>
<Image requestedHeight="40" requestedWidth="40" url="@/resources/computer.png" />
</image>
</ImageView>
<Text text="Host" />
</children>
</HBox>
<HBox>
<children>
<ImageView>
<image>
<Image requestedHeight="40" requestedWidth="40" url="@/resources/server.png" />
</image>
</ImageView>
<Text text="Server" />
</children>
</HBox>
</FXCollections>
</items>
</ListView>
toolbaxController:
public class ToolboxController {
@FXML
private ListView<FXCollections> source;
public static DataFormat dataFormat = new DataFormat("myCell");
@FXML
private void handleDragDetected(MouseEvent event)
{
System.out.println("onDragDetected");
Dragboard db=source.startDragAndDrop(TransferMode.MOVE);
int id=source.getSelectionModel().getSelectedIndex();
ClipboardContent content = new ClipboardContent();
content.put(dataFormat,id);
db.setContent(content);
event.consume();
}
@FXML
void handleDragDone(DragEvent event)
{
System.out.println("onDragDone");
if(event.getTransferMode()==TransferMode.MOVE)
{
// source
}
event.consume();
}
}
main.fxml:
<center>
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<ImageView onDragDropped="#handleDragDrop" onDragEntered="#handleDragEntered" onDragExited="#handleDragExited" onDragOver="#handleDragOver" fx:id="target" fitHeight="253.0" fitWidth="320.0" layoutX="42.0" layoutY="80.0" pickOnBounds="true" preserveRatio="true" />
</children></AnchorPane>
mainController:
public class MainController {
@FXML
private ImageView target;
@FXML
void handleDragOver(DragEvent event)
{
System.out.println("onDragOver");
if (event.getGestureSource() != target &&
event.getDragboard().hasString()) {
target.setStyle("-fx-background-color: aqua");
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.MOVE);
}
event.consume();
}
@FXML void handleDragEntered(DragEvent event)
{
System.out.println("onDragEntered");
if (event.getGestureSource() != target &&
event.getDragboard().hasString()) {
target.setStyle("-fx-background-color:brown");
}
event.consume();
}
@FXML
void handleDragExited(DragEvent event)
{
System.out.println("onDragExited");
target.setStyle("-fx-background-color:darkblue");
event.consume();
}
@FXML
public void handleDragDrop(DragEvent event)
{
System.out.println("onDragDrop");
Object obj;
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasContent(ToolboxController.dataFormat)){
obj=db.getContent(ToolboxController.dataFormat);
success = true;
System.out.println("onDragDrop");
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
}
}
und seine meinem Ausgang, wenn ich einen Artikel von Listview auf Image ziehen:
onDragDetected
onDragEntered
onDragOver
onDragOver
onDragOver
onDragOver
onDragExited
was bedeutet, dass handleDragDrop nicht ausgeführt wird. Ich frage mich, ob das Problem vielleicht ein Ziel ist, ich meine, vielleicht darf das Ziel nicht AnchorPane oder ImageView sein.