Why ActionListener on JButton does not work in Swing in Kotlin?

37 Views Asked by At

I am learning Swing and Kotlin. I want to change text of the button. I made ActionListener on JButton, but when I compile Kotlin project, run by javaw.exe and click on the button, it does not change its text (but it works in Java!). I searched problem in Yandex and YouTube, but there are few of tutorials where there are no proofs that code is working. What is the problem? May be Kotlin is incompatible with Swing?

And yet another problem. When I run code, I see blank window. And when I change its size, then the button appeared. And in Java, and in Kotlin that problem exists. How to solve it?

Here is my code:

import javax.swing.*
import java.awt.Color
import java.awt.*
fun main() {
    val frame = JFrame("Example")
    frame.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
    frame.setSize(400, 300)
    frame.isVisible = true

    val button = JButton("Please click me");
    button.addActionListener {
        button.setText("10 + 20")
    }
    frame.add(button);
}

OpenJDK 21.0.2, Kotlin 1.9.23-release-779, Windows 10 64bit

P. S. Please don't dislike me, because I am beginner in Swing and Kotlin.

What did I done:

  1. I changed from text to setText

  2. I wrote addActionListener by many methods:

    • button.addActionListener {button.text = "10 + 20"}

    • button.addActionListener() {button.text = "10 + 20"}

    • button.addActionListener({button.text = "10 + 20"})

  3. I removed lambda-expression: from button.addActionListener{e -> button.setText("10+20")} to

    1. button.addActionListener{button.setText("10+20") }

Expected behavior: changing button's text to "10+20"

EDIT:

When I executed my code by java.exe, I got next message in console:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics                
        at AwtrunKt.main$lambda$0(Awtrun.kt)                                                                         
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)                     
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2314)                 
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:407)              
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)                       
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)       
        at java.desktop/java.awt.Component.processMouseEvent(Component.java:6621)                                    
        at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)                               
        at java.desktop/java.awt.Component.processEvent(Component.java:6386)                                         
        at java.desktop/java.awt.Container.processEvent(Container.java:2266)                                         
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4996)                                    
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)                                    
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828)                                        
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)                       
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)                        
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)                            
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)                                    
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)                                          
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828)                                        
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)                                   
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)                                               
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)                                               
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)                          
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)                                                                                                              
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)                                                                                                              
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)                                               
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)                                               
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)                          
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)                                                                                                              
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)                                       
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)            
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)               
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)            
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)                        
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)                        
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)                                
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics                                          
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)                   
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)                
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)                                           
        ... 36 more

What is it?!

0

There are 0 best solutions below