(Reposted with enhanced code formatting) Trying to adopt event handlers from convenience methods, but I'm facing undesired behavior. Structure is simple: I have a FlowPane with bunch of VBox nodes.
I'm applying a Select / De-Select mechanism. As I click on a VBox node it gets selected, similarly as I click on empty space in FlowPane area, all nodes gets de-selected.
I'd like to use convenience methods for selecting VBox nodes , which reduces amount of code at the handler (node selector) side ie:
But this is not exclusive event since the FlowPane convenience method gets triggered as well, which would mean a de-selection event right after the selection event. As a workaround I've implemented Mouse Events for the caller VBox Node and this will not trigger the FlowPane convenience method's event.
Is this because of some event hierarchy rule base? Code is below (Grey VBoxes behave as desired (with Mouse Event) whereas Cyan VBox nodes clicked trigger also FlowPane convenience method event handler (not desired).
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.layout.FlowPane
import scalafx.scene.Scene
import scalafx.scene.layout.VBox
import scalafx.geometry.Insets
import scalafx.scene.input.{MouseButton, MouseEvent}
import scalafx.scene.paint.Color
import scalafx.scene.shape.Rectangle
object EventPriority extends JFXApp {
// -------------------------------------------------------------------------------------------100
private def vBoxGreyMouseEvents(videoNode: VBox): Unit = {
videoNode.onMouseClicked = (me: MouseEvent) => {
me.button match {
case MouseButton.Primary => println("VBox: Primary mouse button")
case _ =>
}
me.consume()
}
}
// -------------------------------------------------------------------------------------------100
private def vBoxCyanHandler(flowPNode: VBox): Unit = {
println("from VBox")
//
}
// -------------------------------------------------------------------------------------------100
private def vboxCYANWithConvenience: VBox = new VBox {
padding = Insets(2, 5, 8, 5)
children = new Rectangle {
width = 250
height = 125
fill = Color.Cyan
}
val self: VBox = this
onMouseClicked = (_: MouseEvent) => vBoxCyanHandler(self)
}
// -------------------------------------------------------------------------------------------100
private def vboxGREYWithMouseEV: VBox = new VBox {
padding = Insets(2, 5, 8, 5)
children = new Rectangle {
width = 250
height = 125
fill = Color.Grey
}
val self: VBox = this
vBoxGreyMouseEvents(self)
}
// -------------------------------------------------------------------------------------------100
private def flowPaneHandler(flowPNode: FlowPane): Unit = {
println("from FlowPane")
}
// -------------------------------------------------------------------------------------------100
private def fpaneContent: FlowPane = {
val flowpane = new FlowPane {
for (ix <- 1 until 4) {
children += vboxCYANWithConvenience
children += vboxGREYWithMouseEV
}
}
flowpane.onMouseClicked = (_: MouseEvent) => flowPaneHandler(flowpane)// flowPNodeEventHandler(videoFlowPane)
flowpane
}
// -------------------------------------------------------------------------------------------100
stage = new PrimaryStage {
scene = new Scene(600, 200) {
root = fpaneContent
}
}
} // END