import cn.hutool.core.io.resource.ClassPathResource
import javafx.collections.FXCollections
import tornadofx.*
class LearnApp : App(LearnV::class)
class LearnV : View("learn") {
val lv = this
val cityList = FXCollections.observableArrayList<City>()
val selectedCity = objectProperty<City>()
override val root = borderpane {
center = form {
fieldset {
field {
listview(cityList) {
selectedCity.bind(selectionModel.selectedItemProperty())
}
}
}
}
bottom =vbox{
form {
fieldset {
field("item selected:") {
label(selectedCity)
}
}
}
toolbar {
Buttons.values().forEach { ss ->
button(ss.toString()) {
action {
ss.run(lv)
}
useMaxWidth = true
}
}
}
}
prefWidth = 200.0
prefHeight = 200.0
}
}
enum class Buttons {
Clear {
override fun run(v: LearnV) {
v.cityList.clear()
}
},
Add {
override fun run(v: LearnV) {
v.cityList.addAll(Cities.cities)
}
override fun toString(): String {
return "load list items"
}
};
abstract fun run(v: LearnV)
}
class City(val id: Int, val name: String) {
override fun toString() = name
}
object Cities {
val citiesById = ClassPathResource("cities.csv").file.readText().lines()
.asSequence()
.map { it.split(",") }
.map { City(it[0].toInt(), it[1]) }
.map { it.id to it }
.toMap()
val cities = citiesById.values.toList()
}
文件cities.cvs:
0,Belgrade,717.0,707.0
1,Barcelona,305.0,810.0
2,Berlin,568.0,509.0
3,Brussels,399.0,570.0
4,Bucharest,879.0,698.0
5,Budapest,701.0,649.0
6,Copenhagen,537.0,453.0
7,Dublin,203.0,478.0
8,Hamburg,501.0,495.0
9,Istanbul,932.0,781.0
10,Kiev,900.0,544.0
11,London,318.0,544.0
12,Madrid,182.0,809.0
13,Milan,499.0,713.0
14,Moscow,980.0,304.0
15,Munich,521.0,637.0
16,Paris,343.0,622.0
17,Prague,614.0,587.0
18,Rome,560.0,802.0
19,Saint Petersburg,824.0,305.0
20,Sofia,801.0,760.0
21,Stockholm,631.0,354.0
22,Vienna,616.0,637.0
23,Warsaw,707.0,522.0
24,Reykjavik,95.0,165.0
25,Amsterdam,424.0,536.0