Skip to content

Instantly share code, notes, and snippets.

@bjonnh
Created February 5, 2019 17:03
Show Gist options
  • Save bjonnh/f1dd8d5810c1958c23abd5801711811d to your computer and use it in GitHub Desktop.
Save bjonnh/f1dd8d5810c1958c23abd5801711811d to your computer and use it in GitHub Desktop.
package net.nprod.failuretable
import javafx.beans.property.SimpleListProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.scene.control.TableView
import javafx.scene.paint.Color
import javafx.stage.Stage
import tornadofx.*
import java.time.LocalDate
import java.time.Period
import kotlin.reflect.KClass
class DarkTheme : Stylesheet() {
init {
tableRowCell {
borderColor += box(Color.color(0.2, 0.2, 0.7))
}
}
}
class LightTheme : Stylesheet() {
init {
root {
backgroundColor += Color.LIGHTCYAN
}
}
}
class Person(val id: Int, val name: String, val birthday: LocalDate) {
val age: Int get() = Period.between(birthday, LocalDate.now()).years
}
private val persons = listOf(
Person(1,"Samantha Stuart", LocalDate.of(1981,12,4)),
Person(2,"Tom Marks",LocalDate.of(2001,1,23)),
Person(3,"Stuart Gills",LocalDate.of(1989,5,23)),
Person(3,"Nicole Williams",LocalDate.of(1998,8,11))
).observable()
class ResultFragment : Fragment() {
var table: TableView<Person> by singleAssign()
val settings: ThemeController by inject()
override val root = vbox {
form {
fieldset("Theme") {
field {
vbox {
togglegroup {
// One radio button for each theme, with their value set as the theme
settings.themes.forEach { theme ->
radiobutton(theme.simpleName, getToggleGroup(), theme)
}
// The toggle group value is bound to the activeThemeProperty
bind(settings.activeThemeProperty)
}
}
}
}
}
table = tableview(persons)
{
readonlyColumn("ID",Person::id)
readonlyColumn("Name", Person::name)
readonlyColumn("Birthday", Person::birthday)
readonlyColumn("Age",Person::age)
}
add(table)
}
}
class MainView : View("Failure tableview") {
override val root = splitpane {
add(find<ResultFragment>())
}
}
class MyApp : App(MainView::class) {
val themeController: ThemeController by inject()
override fun start(stage: Stage) {
super.start(stage)
// Make sure we initialize the theme selection system on start
themeController.start()
}
}
class ThemeController : Controller() {
// List of available themes
val themes = SimpleListProperty<KClass<out Stylesheet>>(listOf(LightTheme::class, DarkTheme::class).observable())
// Property holding the active theme
val activeThemeProperty = SimpleObjectProperty<KClass<out Stylesheet>>()
var activeTheme by activeThemeProperty
fun start() {
// Remove old theme, add new theme on change
activeThemeProperty.addListener { _, oldTheme, newTheme ->
oldTheme?.let { removeStylesheet(it) }
newTheme?.let { importStylesheet(it) }
}
// Activate the first theme, triggering the listener above
activeTheme = themes.first()
}
}
fun main(args: Array<String>) {
launch<MyApp>(args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment