What I am trying to accomplish is: having a ScalaFX application with some nice ordered object
s called Buttons
, Labels
, Checkboxes
and so on to keep everything nice and in order.
Here a little example to show what I mean:
package ButtonsAndLabel
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control.{ Button, Label }
import scalafx.event.ActionEvent
object Main extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "Test-Program"
scene = new Scene(300, 200) {
val label = new Label("Nothing happened yet") {
layoutX = 20
layoutY = 20
}
val button1 = new Button("Button 1") {
layoutX = 20
layoutY = 50
onAction = (e: ActionEvent) => {
label.text = "B1 klicked"
}
}
val button2 = new Button("Button 2") {
layoutX = 20
layoutY = 80
onAction = (e: ActionEvent) => {
label.text = "B2 klicked"
}
}
content = List(label, button1, button2)
}
}
}
This code shows a window with a label and two buttons, and the buttons change the text of the label.
That works fine.
But when my code grows with a lot more controls, things get messy.
That's why I tried to transfer the controls into other object
s (in different files). I've put the label into an object called Labels
:
package ButtonsAndLabel
import scalafx.scene.control.Label
import scalafx.event.ActionEvent
object Labels {
val label = new Label("Nothing happened yet") {
layoutX = 20
layoutY = 20
}
}
when I import this into the main-file with
import Labels.label
everything works fine.
But then I try to put the buttons into a Buttons
object:
package ButtonsAndLabel
import scalafx.scene.control.Button
import scalafx.event.ActionEvent
import Labels.label
object Buttons {
val button1 = new Button("Button 1") {
layoutX = 20
layoutY = 50
onAction = (e: ActionEvent) => {
label.text = "B1 klicked"
}
}
val button2 = new Button("Button 2") {
layoutX = 20
layoutY = 80
onAction = (e: ActionEvent) => {
label.text = "B2 klicked"
}
}
}
this brings the error message when I try to compile:
[error] found : scalafx.event.ActionEvent => Unit [error] required: javafx.event.EventHandler[javafx.event.ActionEvent] [error] onAction = (e: ActionEvent) => {
and now I am stuck, as I don't know any Java.
Does anybody know if it is even possible what I am trying to do?
So far I have not found anything about this on the net. The problem doesn't keep me from writing the program I want, but the last application I wrote was a real mess with all the controls in one file.
Am I overlooking something obvious here?
Any help would be really appreciated.