Skip to content

Instantly share code, notes, and snippets.

@kururu-abdo
Created January 26, 2019 21:37
Show Gist options
  • Save kururu-abdo/4da2f1512ba140e5df077b449ddf4110 to your computer and use it in GitHub Desktop.
Save kururu-abdo/4da2f1512ba140e5df077b449ddf4110 to your computer and use it in GitHub Desktop.
how to auto fill combobox in tornadofx
package com.example.demo.view
import org.junit.Assert
import org.junit.Test
import org.testfx.api.FxToolkit
import org.junit.Assert.*
import javafx.beans.property.SimpleStringProperty
import javafx.scene.Node
import javafx.scene.control.*
import javafx.scene.layout.Background
import javafx.scene.layout.BackgroundFill
import javafx.scene.layout.BackgroundSize
import javafx.scene.paint.Color
import javafx.scene.text.Font
import tornadofx.*
import tornadofx.controlsfx.statusbar
import java.io.FileInputStream
import javax.json.JsonObject
import org.junit.Assert.*
class testAuto : View("My View",Button("op")) {
var js="""
[
{"name":"abdo"
"password":"796990"
]
""".trimIndent()
private var nmtxt: TextField by singleAssign()
var pstxt:PasswordField by singleAssign()
var model =personModel()
override val root = form {
fieldset {
this.style{
backgroundColor+=Color.CADETBLUE
font= Font.loadFont(FileInputStream("F:\\Fonts\\LHANDW.TTF"),20.0)
}
field("username") {
textfield(model.name) {
nmtxt=this
}.required()
}
field("password") {
passwordfield(model.password) {
pstxt=this
}.required()
}
button("Log in")
{
useMaxSize = true
style {
backgroundColor += Color.GREEN
}
action{
model.commit()
}
}
}
}
private var context=ValidationContext()
var nmval=context.addValidator(nmtxt,nmtxt.textProperty()){
if(it!!.length<5) error("short name , please check name") else null
}
var psval=context.addValidator(pstxt,pstxt.textProperty()){
if(it!!.contains("2012") ) null else {error("invalid password ,plz check password")}
if (it!!.contains("2012")) success("great") else warning("oops")
}
}
class person{
var name=SimpleStringProperty()
var nmProperty by name
var password =SimpleStringProperty()
var pass by password
}
class personModel: ItemViewModel<person>(){
var name=bind(person::nmProperty)
var password=bind(person::pass)
}
@kururu-abdo
Copy link
Author

@edvin (:

@kururu-abdo
Copy link
Author

Ooops!

this is a code

package com.example.demo.view

import javafx.beans.property.SimpleStringProperty
import javafx.collections.ObservableList
import javafx.scene.control.ComboBox
import javafx.scene.control.SelectionMode
import javafx.scene.control.SelectionModel
import tornadofx.*
import java.io.FileInputStream

class testcss : View("test css") {
var ic=icons()
init {

    icon=ic.googleIcon
}
var comb1:ComboBox<String> by singleAssign()
var loc2 = listOf(stat("khartoum", listOf("umbada", "bahri", "jabel awliaa")), stat("nahr al-neel", listOf("atbara"))).observable()
var ct = loc2.map { it.name.trim() }
var xc: ObservableList<String>? = listOf("choose city").observable()
var str:String?="khartoum"
var l = SimpleStringProperty()
var s = SimpleStringProperty(ct.first())
override val root = vbox {
   comb1= combobox(s, ct) {
        makeAutocompletable()

selectionModel.selectedItemProperty().onChange {
str = selectionModel.selectedItem!!.toString()
}
print(locss(str!!))
println(str)

    }





    combobox(values = xc) {

        makeAutocompletable()

comb1.selectionModel.selectedItemProperty().onChange {
items=locss(str!!)
}

    }

}

fun locss(s: String):ObservableList<String>? {
    xc = when (s) {
        "khartoum"-> listOf("bahri", "kassala", "umbad").observable()
        "nahr al-neel"-> listOf("fifi", "hyigoi", "gggooiho").observable()
        else -> listOf("konakre","melborne").observable()
    }

return xc?.observable()
}
}
package com.example.demo.view

import javafx.beans.property.SimpleStringProperty
import javafx.collections.ObservableList
import javafx.scene.control.ComboBox
import javafx.scene.control.SelectionMode
import javafx.scene.control.SelectionModel
import tornadofx.*
import java.io.FileInputStream

class testcss : View("test css") {
var ic=icons()
init {

    icon=ic.googleIcon
}
var comb1:ComboBox<String> by singleAssign()
var loc2 = listOf(stat("khartoum", listOf("umbada", "bahri", "jabel awliaa")), stat("nahr al-neel", listOf("atbara"))).observable()
var ct = loc2.map { it.name.trim() }
var xc: ObservableList<String>? = listOf("choose city").observable()
var str:String?="khartoum"
var l = SimpleStringProperty()
var s = SimpleStringProperty(ct.first())
override val root = vbox {
   comb1= combobox(s, ct) {
        makeAutocompletable()

selectionModel.selectedItemProperty().onChange {
str = selectionModel.selectedItem!!.toString()
}
print(locss(str!!))
println(str)

    }





    combobox(values = xc) {

        makeAutocompletable()

comb1.selectionModel.selectedItemProperty().onChange {
items=locss(str!!)
}

    }

}

fun locss(s: String):ObservableList<String>? {
    xc = when (s) {
        "khartoum"-> listOf("bahri", "kassala", "umbad").observable()
        "nahr al-neel"-> listOf("fifi", "hyigoi", "gggooiho").observable()
        else -> listOf("konakre","melborne").observable()
    }

return xc?.observable()
}
}
data class stat(var name:String, var local:List)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment