The Programmers Guide To Kotlin - Using Swing
Written by Mike James   
Monday, 26 February 2024
Article Index
The Programmers Guide To Kotlin - Using Swing
Getters & Setters
Swing Events
The Program

At the moment pure Kotlin programs that target the JVM and make no use of Java code are are rare. In this extract from the book on Kotlin by Mike James we look at how to use a general Java library - Swing - to use Java code within Kotlin programs.

Programmer's Guide To Kotlin Second Edition

kotlin2e360

You can buy it from: Amazon

Contents

  1. What makes Kotlin Special
  2. The Basics:Variables,Primitive Types and Functions 
  3. Control
         Extract: If and When 
  4. Strings and Arrays
  5. The Class & The Object
  6. Inheritance
  7. The Type Hierarchy
  8. Generics
  9. Collections, Iterators, Sequences & Ranges
        Extract: Iterators & Sequences 
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
  12. Data classes, enums and destructuring
        Extract: Destructuring 
  13. Exceptions, Annotations & Reflection
  14. Coroutines
        Extract: Coroutines 
  15. Working with Java
        Extract: Using Swing ***NEW!

<ASIN:B096MZY7JM>

Using Swing

In this chapter we are going to concentrate on using Java code from Kotlin rather than the other way round. The reason for this orientation is simply that there is a lot of existing Java code out there, and Kotlin generally has to make use of it in the form of precompiled libraries. The situation of needing to use Kotlin code from Java is much less common and, at the moment, is usually a design choice.

On the other hand, the need to use existing Java code is almost essential to any use of Kotlin. For example, you can opt to create complete Android applications by writing nothing but Kotlin, but as the Android libraries are all written in Java you have no choice but to discover how to work with Java objects and their methods and properties. Even a 100% Kotlin Android program has to interact with the system's Java.

If you are starting a new Kotlin project in a general environment then you are going to need to use Java libraries such as Swing or JavaFX to build your user interface. At the moment pure Kotlin programs that target the JVM and make no use of Java code are are rare.

The good news is that Kotlin compiles to byte code that is compatible with Java 6 (you can opt for later versions of Java). What this means is that all of Kotlin's advanced syntax is mostly that – syntax which disappears when compiled. It is because Kotlin uses the same byte code and the same overall structures and libraries that makes it possible for it to claim 100% Java compatibility.

For this reason you can start out with the optimistic view that if you want to use an existing Java library you can. The only problems you might have is finding the right Kotlin syntax to make the correct connection with the Java code. Most of the time it is simply a matter of not using some new feature that Kotlin has introduced or finding out how the new feature maps onto something that exists in Java.

In this chapter we are going to use the Swing GUI library as an example of using Java code. The main reason is that Kotlin doesn't have a GUI library of its own, and while there are newer Java GUI libraries, Swing is still worth using.

If you want to know more about using Android Java code in Kotlin see: Android Programming In Kotlin: Starting with an App ISBN: 978-1871962543.

Using Java Classes

The most important thing to realize is that Java and Kotlin use the same class implementation at run time – because they both use the JVM which supports classes and objects at a fairly high level. You can import any Java class and create instances using Kotlin syntax, i.e. no need for new.

For example, to create an instance of the Swing JFrame class all you have to write is:

val myFrame=JFrame("Hello Word")

You also need to include:

import javax.swing.*

at the start of the file.

If you actually want to see the JFrame then you need to set its size and set it to visible:

myFrame.setSize(300,200)
myFrame.setVisible(true)

If you run the final program:

import javax.swing.*
fun main(args: Array<String>) {
    val myFrame=JFrame("Hello World")
myFrame.setSize(300,200)
myFrame.setVisible(true)
}

You will see a JFrame open with the title Hello World:

helloworld

Notice that creating Swing objects on the main program thread isn't the best way to work, but it is a simple way to get started. See how to do it properly in a later section.

<ASIN:1871962536>

<ASIN:1871962544>



Last Updated ( Monday, 26 February 2024 )