How to run a Java Fx code in Kotlin

Dear Friends,
It’s my 3rd day in Kotlin study. I need to create a GUI with Java Fx and Kotlin. I used below code in intellij IDEA Community edition.

import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.control.TextField
import javafx.scene.layout.VBox
import javafx.stage.Stage

class CalculatorApp : Application() {
    override fun start(primaryStage: Stage) {
        val firstNumberField = TextField()
        val secondNumberField = TextField()
        val calculateButton = Button("Calculate")
        val resultLabel = Label()

        calculateButton.setOnAction {
            val firstNumber = firstNumberField.text.toDoubleOrNull() ?: 0.0
            val secondNumber = secondNumberField.text.toDoubleOrNull() ?: 0.0
            val sum = firstNumber + secondNumber
            resultLabel.text = "The sum of $firstNumber and $secondNumber is $sum"
        }

        val vbox = VBox(10.0)
        vbox.padding = Insets(20.0)
        vbox.children.addAll(
            Label("Enter the first number:"),
            firstNumberField,
            Label("Enter the second number:"),
            secondNumberField,
            calculateButton,
            resultLabel
        )

        val scene = Scene(vbox, 300.0, 250.0)

        primaryStage.title = "Calculator App"
        primaryStage.scene = scene
        primaryStage.show()
    }
}

fun main() {
    Application.launch(CalculatorApp::class.java)
}

But I got a number of errors (25 nos) as

D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:2:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:3:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:4:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:5:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:6:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:7:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:8:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:10:23
Kotlin: Unresolved reference: Application
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:11:5
Kotlin: ‘start’ overrides nothing
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:11:38
Kotlin: Unresolved reference: Stage
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:12:32
Kotlin: Unresolved reference: TextField
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:13:33
Kotlin: Unresolved reference: TextField
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:14:31
Kotlin: Unresolved reference: Button
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:15:27
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:21:25
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:24:20
Kotlin: Unresolved reference: VBox
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:25:14
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:25:24
Kotlin: Unresolved reference: Insets
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:27:13
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:29:13
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:35:21
Kotlin: Unresolved reference: Scene
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:37:22
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:38:22
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:44:5
Kotlin: Unresolved reference: Application

This occur after I download JavaFx and include in Intellij IDEA community Edition .

I followed upto 3 steps (3 not included) in solution to add JavaFX …Link

Please help with a solution

Thanks

Anes P A

These days it’s more common to use a dependency manager to manage libraries: usually Maven or Gradle.

But that’s probably overkill for toy projects, and you can still download a library yourself and then tell IntelliJ to use it — which what the first two steps you link to are doing.

It looks like something is awry there, because your project should then compile without the errors you show. (You’ll need the third step only when you come to actually run your program.)

The problem’s not obvious — and hard to debug remotely. But the next place I’d check would be the Module Settings (in the Project pane, right-click/shift+click on the module, and select Open Module Settings), and check that your library is shown in its Libraries pane — add it if not. You might also need to do a full Build of the module.

Dear @gidds
Now when I look that library nothing shows . But Wen I add the library show dialog as
in attached screenshot


I think it’s not prooper way.

Please advise

Thanks

Anes P A

Calculator.zip (164.2 KB)
Here is a working javafx project with your code - made from javafx project template

Dear @andrewww
Thanks for your support. I need to know a way how to use JavaFX with Kotlin , in that calculator functionality is a way. So please advise how to solve my above issue …

Thanks

Anes P A

That’s odd… Are you sure the SDK was properly downloaded and unzipped, and that you selected the resulting folder (named something like javafx-sdk-20.0.2) when you tried to create the library in IntelliJ?

Errors are clearly because there is no javafx dependency in the project classpath, so problem is in your project setup - reliable project setup is to use gradle build scripts where you see which dependencies you include into your project and gradle automatically downloads everything you required to build project. Sample I provided works, so you can see build.gradle in the root of the project how to include javafx library as dependency of the project

Thanks for all to give support. At last I created my small application in Swing-GUI.
My working code is

import javax.swing.*
import java.awt.*
import java.awt.event.*

class Calculator : JFrame("Calculator"), ActionListener {
    private val input1: JTextField = JTextField(10)
    private val input2: JTextField = JTextField(10)
    private val addButton: JButton = JButton("+")
    private val multiplyButton: JButton = JButton("*")
    private val resultLabel: JLabel = JLabel("Result: ")

    init {
        layout = GridLayout(3, 2)
        add(JLabel("Input 1: "))
        add(input1)
        add(JLabel("Input 2: "))
        add(input2)
        add(addButton)
        add(multiplyButton)
        add(resultLabel)
        addButton.addActionListener(this)
        multiplyButton.addActionListener(this)
        pack()
        isVisible = true
    }

    override fun actionPerformed(e: ActionEvent) {
        val input1Value = input1.text.toDouble()
        val input2Value = input2.text.toDouble()
        val result: Double = when (e.source) {
            addButton -> input1Value + input2Value
            multiplyButton -> input1Value * input2Value
            else -> throw IllegalArgumentException("Invalid button")
        }
        resultLabel.text = "Result: $result"
    }
}

fun main() {
    Calculator()
}

Closing my Thread here…

Thanks

Anes P A