I have a Pane into which I want to move files, but for some reason it doesn’t trigger on these events, I’m already confused, the pane processes all events except setOnDragOver, setOnDragEntered, setOnDragExited, setOnDragDropped
val drag = Pane().apply {
id = "dragFile"
layoutX = 28.0
layoutY = 58.0
val dragImage = ImageView().apply {
id = "drag"
fitWidth = 80.0
fitHeight = 80.0
layoutX = 60.0
layoutY = 20.0
}
val dragText = Label("text").apply {
id = "dragText"
layoutX = 29.0
layoutY = 100.0
}
children.addAll(dragImage, dragText)
setOnDragOver { event ->
if (event.dragboard.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY)
}
event.consume()
}
setOnDragEntered { event ->
if (event.dragboard.hasFiles()) {
id = "dragFileActive"
}
event.consume()
}
setOnDragExited { _ -> id = "dragFile" }
setOnDragDropped { event ->
if (event.dragboard.hasFiles()) {
event.isDropCompleted = true
handler(event.dragboard.files)
}
event.consume()
}
}
I tried outputting a message to the console, even that didn’t work.
setOnDragOver { println("drag over") }
setOnDragEntered { println("drag entered") }
setOnDragExited { println("drag exited") }
setOnDragDropped { println("drag drop") }
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.input.TransferMode
import javafx.scene.layout.Pane
import javafx.stage.Stage
import java.io.File
class FileDropApp : Application() {
override fun start(primaryStage: Stage) {
val pane = Pane().apply {
id = "importFile"
layoutX = 273.0
layoutY = 135.0
prefWidth = 400.0
prefHeight = 300.0
style = "-fx-background-color: lightgray;"
val dropArea = Pane().apply {
id = "dragFile"
layoutX = 50.0
layoutY = 50.0
prefWidth = 300.0
prefHeight = 200.0
style = "-fx-background-color: white; -fx-border-color: black;"
setOnDragOver { event ->
if (event.dragboard.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY)
}
event.consume()
}
setOnDragEntered { event ->
if (event.dragboard.hasFiles()) {
style = "-fx-background-color: lightgreen; -fx-border-color: black;"
}
}
setOnDragExited { _ -> style = "-fx-background-color: white; -fx-border-color: black;" }
setOnDragDropped { event ->
if (event.dragboard.hasFiles()) {
val files = event.dragboard.files
event.isDropCompleted = true
handleFiles(files)
} else {
event.isDropCompleted = false
}
event.consume()
}
}
children.addAll(dropArea)
}
val scene = Scene(pane, 800.0, 600.0)
primaryStage.title = "File Drop Example"
primaryStage.scene = scene
primaryStage.show()
}
private fun handleFiles(files: List<File>) {
files.forEach { file ->
println("File dropped: ${file.absolutePath}")
}
}
}
fun main() {
Application.launch(FileDropApp::class.java)
}
Preface: You've confirmed in a comment that running your application "as an administrator" was the source of your problem, so I'm moving my guess to an answer.
So, your problem is with dragging files from your desktop to your application but your application isn't receiving any events. Given your code works for others on multiple operating systems, including the one you're using, your problem is likely something to do with your environment specifically rather than the code itself. Now, there isn't really enough information in your question to give a definitive answer, but since the problem is specifically with drag-and-drop on Windows there is a likely reason:
You're running your application with administrative privileges.
On Windows, drag-and-drop from a non-elevated application to an elevated one is not allowed. And the desktop's process is not elevated. There may be similar rules on other operating systems, I'm not sure.
Though I assume you're not intentionally running your application with administrative privileges. But if you launch your application from an elevated process then your application will inherit the elevated state, at least by default. Check to see if your IDE, or whatever you're using to launch your application, is elevated. If it is, then try relaunching your IDE (or whatever) without administrative privileges. Or, if you need your IDE (or whatever) to be elevated for some reason, try launching your application another way (e.g., from a terminal that isn't running as administrator).