Easily create Kotlin based JavaFX using boilerplate Gradle DSL
A Simple JavaFX code without installed JavaFX can be done.
In gradle the library marked with a name openjfx(<library>)
openjfx("org.openjfx:javafx-base:14:${platform}")
This openjfx can be used in Copy step as below
from(openjfx)
Kotlin Code
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Parent
import javafx.scene.Scene
import javafx.stage.Stage
class App : Application() {
public override fun start(primaryStage:Stage) {
try {
var root:Parent = FXMLLoader.load(App::class.java.getResource("App.fxml"));
var scene:Scene = Scene(root)
scene.getStylesheets().add(App::class.java.getResource("App.css").toExternalForm())
primaryStage.setTitle("Example")
primaryStage.setScene(scene)
primaryStage.show()
} catch (e:Exception) {
e.printStackTrace()
}
}
companion object {
fun main(args: Array<String>) {
}
}
}
FXML Code
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
<children>
<VBox AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox VBox.vgrow="ALWAYS">
<children>
<TextArea fx:id="box1" HBox.hgrow="ALWAYS" />
<TextArea fx:id="box2" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
<BorderPane maxHeight="50.0" minHeight="50.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0">
<center>
<Button fx:id="mybutton" maxHeight="50"
mnemonicParsing="false" prefHeight="50.0"
prefWidth="200.0" text="Button" visible="false" />
</center>
</BorderPane>
</children>
</AnchorPane>
run the gradle command
gradlew run
That's All!!
Gradle dependency named configuration
val openjfx by configurations.creating
Gradle Dependency
implementation("org.openjfx:javafx-base:14:${platform}")
implementation("org.openjfx:javafx-graphics:14:${platform}")
implementation("org.openjfx:javafx-controls:14:${platform}")
implementation("org.openjfx:javafx-fxml:14:${platform}")
implementation("org.openjfx:javafx-media:14:${platform}")
implementation("org.openjfx:javafx-web:14:${platform}")
openjfx("org.openjfx:javafx-base:14:${platform}")
openjfx("org.openjfx:javafx-graphics:14:${platform}")
openjfx("org.openjfx:javafx-controls:14:${platform}")
openjfx("org.openjfx:javafx-fxml:14:${platform}")
openjfx("org.openjfx:javafx-media:14:${platform}")
openjfx("org.openjfx:javafx-web:14:${platform}")
Get the OpenJFX to local directory before invoking java command
tasks.withType<JavaExec> {
main = "com.zigma.App"
dependsOn("copyFXLibs")
}
register("copyFXLibs", Copy::class) {
from(openjfx)
into("fxlib")
}
JVM arguments
application {
mainClassName = "com.zigma.App"
applicationDefaultJvmArgs = listOf(
"--module-path=fxlib",
"--add-modules=javafx.controls,javafx.fxml,javafx.web"
)
}
End Of Article