Kotlin.swing (newbie question)


#1

Hello,

I was hoping to try building my first Swing application, which would also be my first real Kotlin application. I’ve installed the Kotlin plugin for IntelliJ and have been going through the Koans without much trouble.
With this new project, I have the following issue where the “swing” in kotlin.swing.* below is in red due to “Unresolved reference: swing”.

import javax.swing.*
import kotlin.swing.*

Is kotlin.swing not included as part of the standard library shipped with the plugin? Do I need to build kotlin-libraries and add the appropriate jars to the project, or is there a place to download a prebuilt library?

Thanks!


#2

You need to add a dependency to kotlin-swing (http://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-swing), but I woudn't really recommend it. It's very small library (https://github.com/JetBrains/kotlin/tree/master/libraries/kotlin-swing/src/main/kotlin/kotlin/swing) not doing much for Swing, created several years ago and only maintained to compile, and we think we will just drop it soon. Another reason is that Swing is not very well supported on modern Mac OS X. You may want to look at KotlinFX (https://github.com/eugenkiss/kotlinfx), or just use normal Kotlin's Java interop with Swing. Or, may be, develop own Swing DSL in Kotlin, it's not really hard :)


#3

Thanks for all the suggestions. I went with KotlinFX; this was the first I realized JavaFX was now meant as a desktop GUI library as well. However, using gradle, I seem to have some difficulties in getting started. It looks like there may be a version mismatch between the kotlin task and the version of gradle I have installed:

$ ~/gradle-2.2.1/bin/gradle --stacktrace  # Edit: I also just tried the latest gradle (2.3) and the issue remains the same

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘C:cygwin64homebrand_000LaRbuild.gradle’ line: 30

  • What went wrong:
    A problem occurred evaluating root project ‘LaR’.
    > Failed to apply plugin [id ‘kotlin’]
      > java.lang.reflect.InvocationTargetException (no error message)

  • Try:
    Run with --info or --debug option to get more log output.

  • Exception is:
    org.gradle.api.GradleScriptException: A problem occurred evaluating root project ‘LaR’.
      at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)
      at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:148)
      at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:39)
      at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:26)
      at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
      at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:59)
      at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:504)
      at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:83)
      at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:42)
      at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
      at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:129)
      at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
      at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
      at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)
      at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
      at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
      at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)
      at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
      at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
      at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)
      at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
      at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
      at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
      at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
      at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
      at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
      at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
      at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
      at org.gradle.launcher.Main.doAction(Main.java:33)
      at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
      at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
      at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
      at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
    Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id ‘kotlin’]
      at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyPlugin(DefaultObjectConfigurationAction.java:117)
      at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.access$200(DefaultObjectConfigurationAction.java:36)
      at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction$3.run(DefaultObjectConfigurationAction.java:80)
      at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.execute(DefaultObjectConfigurationAction.java:131)
      at org.gradle.api.internal.project.AbstractPluginAware.apply(AbstractPluginAware.java:37)
      at org.gradle.api.Project$apply$0.call(Unknown Source)
      at org.gradle.api.internal.project.ProjectScript.apply(ProjectScript.groovy:34)
      at org.gradle.api.Script$apply$0.callCurrent(Unknown Source)
      at build_1ixj5q307q49m25w4qo6wudyn.run(C:cygwin64homebrand_000LaRbuild.gradle:30)
      at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:52)
      … 32 more
    Caused by: java.lang.reflect.InvocationTargetException
      at org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper.apply(KotlinPluginWrapper.kt:64)
      at org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper.apply(KotlinPluginWrapper.kt)
      at org.gradle.api.internal.plugins.DefaultPluginContainer.providePlugin(DefaultPluginContainer.java:188)
      at org.gradle.api.internal.plugins.DefaultPluginContainer.addPluginInternal(DefaultPluginContainer.java:137)
      at org.gradle.api.internal.plugins.DefaultPluginContainer.apply(DefaultPluginContainer.java:103)
      at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyPlugin(DefaultObjectConfigurationAction.java:115)
      … 41 more
    Caused by: java.lang.NoSuchMethodError: org.gradle.api.internal.tasks.TaskContainerInternal.add(Ljava/lang/String;Ljava/lang/Class;)Lorg/gradle/api/Task;
      at org.jetbrains.kotlin.gradle.plugin.KotlinPlugin$configureSourceSetDefaults$1.execute(KotlinPlugin.kt:82)
      at org.jetbrains.kotlin.gradle.plugin.KotlinPlugin$configureSourceSetDefaults$1.execute(KotlinPlugin.kt)
      at org.gradle.api.internal.DefaultDomainObjectCollection.all(DefaultDomainObjectCollection.java:110)
      at org.jetbrains.kotlin.gradle.plugin.KotlinPlugin.configureSourceSetDefaults(KotlinPlugin.kt:65)
      at org.jetbrains.kotlin.gradle.plugin.KotlinPlugin.apply(KotlinPlugin.kt:53)
      … 47 more

BUILD FAILED

Total time: 3.771 secs

I suspect this is another newbie question, but may not be kotlin-specific…

In case it helps and anyone wants to have a look, the code is here (I just copied the kotlinfx hello-world example for now):
https://github.com/cornell-comp-internal/LaR.git

Thanks again,


#4

This issue was addressed with the following patch to kotlinfx: https://github.com/eugenkiss/kotlinfx/commit/90f97fd239811230c2025ef3aaa2563b96f40896

Think I’ve got a few other gralde issues going on but will hopefully get those worked out from reading about gradle more.