Skip to content

Instantly share code, notes, and snippets.

@briangordon
Created September 26, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briangordon/1f9c65f1333fa3355fc41cc8aca78563 to your computer and use it in GitHub Desktop.
Save briangordon/1f9c65f1333fa3355fc41cc8aca78563 to your computer and use it in GitHub Desktop.
package cellfactory
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.scene.control.ListView
import javafx.scene.layout.StackPane
import javafx.scene.{Scene, control => jfxsc}
import javafx.stage.Stage
import javafx.util.Callback
class TestLauncher extends Application {
def start(stage: Stage): Unit = {
val javafxComboBox = new jfxsc.ComboBox[MyCustomClass]()
val cellFactoryCallback = new Callback[jfxsc.ListView[MyCustomClass], jfxsc.ListCell[MyCustomClass]] {
override def call(param: ListView[MyCustomClass]) = new jfxsc.ListCell[MyCustomClass] {
// Customize the appearance of the cell.
override def updateItem(item: MyCustomClass, empty: Boolean): Unit = {
super.updateItem(item, empty)
if (empty || item == null) {
setText(null)
setGraphic(null)
} else {
setText("Good toString")
}
}
}
}
javafxComboBox.setCellFactory(cellFactoryCallback)
javafxComboBox.getSelectionModel.selectFirst()
javafxComboBox.setItems(FXCollections.observableArrayList(MyCustomClass("blah"), MyCustomClass("blahhh")))
val root = new StackPane()
root.getChildren.add(javafxComboBox)
stage.setScene(new Scene(root))
stage.show()
}
}
object TestLauncher {
def main(args: Array[String]): Unit = {
Application.launch(classOf[TestLauncher])
}
}
case class MyCustomClass(str: String) {
override def toString = "Bad toString"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment