Is there a SWING example somewhere?


#1

I can probably try to translate from Java code but if there is an example that employes Kotlin specific feature it'd be great to learn from :)


#2

A very preliminary example can be found here: https://github.com/JetBrains/kotlin/tree/master/examples/src/swing


#3

Thanks Andrey. The two files don't compile though. Here the errors:

IdeaProjectsKotlinSwingTestsrcSwingBuilder.kt:(27,7) Class ‘KFrame’ must override open var minimumSize : Dimension? defined in <java_root>.java.awt.Container because it inherits many implementations of it
IdeaProjectsKotlinSwingTestsrcSwingBuilderTest.kt:(10,9) Val can not be reassigned
IdeaProjectsKotlinSwingTestsrcSwingBuilderTest.kt:(11,9) Val can not be reassigned


#4

Please, watch this issue:

http://youtrack.jetbrains.com/issue/KT-1432


#6

No working example for 5 years. That should tell me something. But I will use Kotlin as a teacher only if there is a GUI-Form-Creator.
My project works as a Java-File. If I change it to Kotlin it seems that the compiler doesn’t find the form.

Exception in thread "main" kotlin.KotlinNullPointerException

My code:

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

class fenster {
    private val myFenster: JPanel? = null
    private val button1: JButton? = null
    private val textField1: JTextField? = null

init {
    button1!!.addActionListener { e -> textField1!!.text = "Hallo Welt" }
}

companion object {

    @JvmStatic
    fun main(args: Array<String>) {
        val frame = JFrame("Sag Hallo")
        frame.contentPane = fenster().myFenster
        frame.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
        frame.pack()
        frame.isVisible = true
    }
}

} `
Mit lieben Grüßen,
Uli


#7

Personally, I do not see any reason to invest in swing. If you are not bound by legacy code, you should use JavaFX which is much more concise and modern (and it is official Java mainstream). For JavaFX, there is a brilliant tornadofx library in kotlin. They have a very good guide and a set of examples.


#8

By the code you posted none of your variables are being initialized. And then you called button1 from init block.
Are they being auto-generated somehow?


#9

Yes, I used the GUI-Form-generator. Looks like my students have to do the form from scratch with tornadofx.
Thanks for trying

Gruß, Uli