Skip to content

Commit d7ed898

Browse files
Add files via upload
0 parents  commit d7ed898

File tree

30 files changed

+2607
-0
lines changed

30 files changed

+2607
-0
lines changed

Ch00_Util/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
plugins {
2+
id 'java-library'
3+
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
4+
id 'org.openjfx.javafxplugin' version '0.0.13'
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
}
13+
14+
compileKotlin {
15+
kotlinOptions {
16+
suppressWarnings = true
17+
jvmTarget = "17"
18+
}
19+
}
20+
21+
javafx {
22+
version = "19"
23+
modules("javafx.controls", "javafx.graphics")
24+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package book.kotlinfx.util
2+
3+
import javafx.scene.control.*
4+
import javafx.beans.property.StringProperty
5+
import javafx.beans.property.SimpleStringProperty
6+
import javafx.beans.binding.StringExpression
7+
import javafx.beans.property.ReadOnlyStringProperty
8+
import javafx.scene.layout.Region
9+
import javafx.scene.layout.GridPane
10+
import javafx.scene.layout.TilePane
11+
import javafx.geometry.Orientation
12+
import javafx.beans.Observable
13+
import javafx.beans.value.ObservableValue
14+
import javafx.scene.text.Text
15+
import javafx.beans.binding.Bindings
16+
import javafx.beans.property.DoubleProperty
17+
import javafx.beans.property.Property
18+
import javafx.beans.property.BooleanProperty
19+
import javafx.scene.Node
20+
import javafx.event.Event
21+
import javafx.event.EventHandler
22+
23+
fun HSTRUT(s:Int) = Region().apply{prefWidth=s.toDouble(); minWidth=s.toDouble()}
24+
fun VSTRUT(s:Int) = Region().apply{prefHeight=s.toDouble(); minHeight=s.toDouble()}
25+
26+
fun Button(label: String, action: Button.() -> Unit): Button =
27+
Button(label).apply {
28+
setOnAction { _ -> action() }
29+
}
30+
31+
fun Label(p:StringExpression):Label = Label().apply{
32+
textProperty().bind(p)
33+
}
34+
35+
fun MenuItem(label: String, action: MenuItem.() -> Unit): MenuItem =
36+
MenuItem(label).apply {
37+
setOnAction { _ -> action() }
38+
}
39+
40+
fun GridPane(hgap:Double, vgap:Double): GridPane =
41+
GridPane().apply {
42+
this.hgap = hgap
43+
this.vgap = vgap
44+
}
45+
46+
fun GridPane(hgap:Int, vgap:Int): GridPane =
47+
GridPane(hgap.toDouble(), vgap.toDouble())
48+
49+
fun TilePane(hgap:Double, vgap:Double): TilePane =
50+
TilePane().apply {
51+
this.hgap = hgap
52+
this.vgap = vgap
53+
}
54+
55+
fun TilePane(hgap:Int, vgap:Int): TilePane =
56+
TilePane(hgap.toDouble(), vgap.toDouble() )
57+
58+
fun TilePane(orientation:Orientation, hgap:Double, vgap:Double): TilePane =
59+
TilePane(orientation).apply {
60+
this.hgap = hgap
61+
this.vgap = vgap
62+
}
63+
64+
fun TilePane(orientation:Orientation, hgap:Int, vgap:Int): TilePane =
65+
TilePane(orientation, hgap.toDouble(), vgap.toDouble())
66+
67+
fun Text(observable:ObservableValue<String>) = Text().apply{ textProperty().bind(observable) }
68+
69+
// Allows for binding Double, ... properties as String observables
70+
fun bindingNumberToString(nmbr:Property<Number>) : ObservableValue<String> =
71+
Bindings.createObjectBinding( { -> nmbr.value.toString() }, nmbr )
72+
73+
fun TextField(sp:StringProperty) = TextField().apply { textProperty().bindBidirectional(sp) }
74+
fun TextArea(sp:StringProperty) = TextArea().apply { textProperty().bindBidirectional(sp) }
75+
fun CheckBox(bp:BooleanProperty) = CheckBox().apply { selectedProperty().bindBidirectional(bp) }
76+
77+
fun ToggleGroup.addMyListener(listener: (value:String) -> Unit) {
78+
selectedToggleProperty().addListener{_,_,newVal ->
79+
listener(newVal.userData as String)
80+
}
81+
}
82+
83+
fun ToggleGroup(listener: (value:String) -> Unit) = ToggleGroup().apply{ addMyListener(listener) }
84+
85+
fun ToggleGroup.selectToggle(id:String) {
86+
selectToggle( toggles.find { it -> it.userData == id } )
87+
}
88+
89+
fun RadioButton(id:String, label:String, toggleGroup:ToggleGroup) = RadioButton(label).apply{
90+
toggleGroup.toggles.add(this)
91+
this.userData = id
92+
}
93+
94+
fun Accordion(pane1:Pair<String,Node>, vararg panes:Pair<String,Node>) = Accordion().apply{
95+
listOf(pane1,*panes).forEach{ pp ->
96+
this.panes.add(TitledPane(pp.first,pp.second))
97+
}
98+
}
99+
100+
fun TabPane(pane1:Pair<String,Node>, vararg panes:Pair<String,Node>) = TabPane().apply{
101+
listOf(pane1,*panes).forEach{ pp ->
102+
this.tabs.add(Tab(pp.first,pp.second))
103+
}
104+
}
105+
106+
fun <T : Event> EH(clos:(event:T) -> Unit): EventHandler<T> = object : EventHandler<T>{ override fun handle(event:T){
107+
clos(event)
108+
}}

Ch01_Intro/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugins {
2+
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
3+
id 'application'
4+
id 'org.openjfx.javafxplugin' version '0.0.13'
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
implementation project(':Ch00_Util')
13+
}
14+
15+
application {
16+
mainClass = 'book.kotlinfx.ch01.AppKt'
17+
}
18+
19+
compileKotlin {
20+
kotlinOptions {
21+
suppressWarnings = true
22+
jvmTarget = "17"
23+
}
24+
}
25+
26+
javafx {
27+
version = "19"
28+
modules("javafx.controls", "javafx.fxml", "javafx.graphics")
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package book.kotlinfx.ch01
2+
3+
import javafx.application.Application
4+
import javafx.event.ActionEvent
5+
import javafx.event.EventHandler
6+
import javafx.scene.Scene
7+
import javafx.scene.control.Button
8+
import javafx.scene.layout.StackPane
9+
import javafx.stage.Stage
10+
11+
import book.kotlinfx.util.* // from Ch00_Util
12+
13+
fun main(args:Array<String>) {
14+
Application.launch(HelloWorld::class.java, *args)
15+
}
16+
17+
class HelloWorld : Application() {
18+
override
19+
fun start(primaryStage:Stage) {
20+
primaryStage.title = "Hello World!"
21+
22+
// val btn = Button().apply {
23+
// text = "Say 'Hello World'"
24+
// setOnAction { _ ->
25+
// println("Hello World!")
26+
// }
27+
// }
28+
// val btn = Button("Say 'Hello World'").apply {
29+
// setOnAction { _ ->
30+
// println("Hello World!")
31+
// }
32+
// }
33+
val btn = Button("Say 'Hello World'"){
34+
println("Hello World!")
35+
}
36+
37+
val root = StackPane().apply {
38+
children.add(btn)
39+
}
40+
41+
with(primaryStage){
42+
scene = Scene(root, 300.0, 250.0)
43+
show()
44+
}
45+
}
46+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package book.kotlinfx.ch01
2+
3+
import javafx.application.Application
4+
import javafx.event.ActionEvent
5+
import javafx.event.EventHandler
6+
import javafx.scene.Scene
7+
import javafx.scene.control.Button
8+
import javafx.scene.text.Text
9+
import javafx.scene.layout.StackPane
10+
import javafx.stage.Stage
11+
import javafx.fxml.FXMLLoader
12+
import javafx.scene.layout.Pane
13+
import javafx.fxml.FXML
14+
import java.lang.SuppressWarnings
15+
16+
fun main(args:Array<String>) {
17+
Application.launch(HelloWorldFXML::class.java, *args)
18+
}
19+
20+
class MyController {
21+
@FXML var actiontarget:Text? = null
22+
23+
@Suppress("UNUSED_PARAMETER")
24+
@FXML fun handleButtonAction( event:ActionEvent) {
25+
actiontarget?.setText("Sign in button pressed")
26+
}
27+
}
28+
29+
/**
30+
* If you want to test this, you must set
31+
* application {
32+
* mainClass = 'book.kotlinfx.ch01.AppFXMLKt'
33+
* }
34+
* in build.gradle
35+
*/
36+
class HelloWorldFXML : Application() {
37+
override
38+
fun start(primaryStage:Stage) {
39+
primaryStage.title = "Hello World!"
40+
41+
val location = this::class.java.classLoader.getResource("helloworld.fxml")
42+
println(location)
43+
//val resources = ResourceBundle.getBundle("com.foo.example")
44+
val fxmlLoader = FXMLLoader(location) //, resources);
45+
46+
val root = fxmlLoader.load<Pane>()
47+
//val controller = fxmlLoader.getController<MyController>();
48+
49+
with(primaryStage){
50+
scene = Scene(root, 300.0, 250.0)
51+
show()
52+
}
53+
}
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import java.net.*?>
4+
<?import javafx.geometry.*?>
5+
<?import javafx.scene.control.*?>
6+
<?import javafx.scene.layout.*?>
7+
<?import javafx.scene.text.*?>
8+
9+
<GridPane fx:controller="book.kotlinfx.ch01.MyController"
10+
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
11+
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
12+
<HBox spacing="10" alignment="bottom_right"
13+
GridPane.columnIndex="1" GridPane.rowIndex="2">
14+
<Button text="Click me"
15+
onAction="#handleButtonAction"/>
16+
</HBox>
17+
18+
<Text fx:id="actiontarget"
19+
GridPane.columnIndex="1" GridPane.rowIndex="4"/>
20+
21+
</GridPane>

Ch02_Properties/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugins {
2+
id 'org.jetbrains.kotlin.jvm' version '1.7.10'
3+
id 'application'
4+
id 'org.openjfx.javafxplugin' version '0.0.13'
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
implementation project(':Ch00_Util')
13+
}
14+
15+
application {
16+
mainClass = 'book.kotlinfx.ch02.AppKt'
17+
//mainClass = 'book.kotlinfx.ch02.AppWithViewModelKt'
18+
}
19+
20+
compileKotlin {
21+
kotlinOptions {
22+
suppressWarnings = true
23+
jvmTarget = "17"
24+
}
25+
}
26+
27+
javafx {
28+
version = "19"
29+
modules("javafx.controls", "javafx.fxml", "javafx.graphics")
30+
}

0 commit comments

Comments
 (0)