August 15th, 2018

Converting List to Enumeration in Kotlin

There’s a bunch of helper extension methods that the Kotlin standard library provides for working with collections. However, it would seem that at the present moment java.util.Enumeration has been left a bit behind. Here is a simple extension method to convert any List to a matching Enumeration:

And here is how you can use it to expose the local file system to the JTree component. First, we create a custom implementation of the TreeNode interface:

Note a few language shortcuts that make the code more concise than its Java counterparts:

  • Since File.listFiles() can return null, we wrap that call with a simple Elvis operator: file.listFiles() ?: arrayOf().
  • The initializer block sorts the File children in place by name.
  • To return tree node enumeration in children(), we first map each File child to the corresponding FileTreeNode and then use our extension function to convert the resulting List to Enumeration.
  • Looking up the index of the specific node is done with the existing extension indexOfFirst function from the standard library.

Now all is left to do is to create our JTree:

August 7th, 2018

Replacing SwingWorker with Kotlin coroutines

Kotlin 1.3 is right around the corner, and one of the major additions to the language is going to be the official graduation of coroutines from experimental to stable. Let’s take a look at how we can replace the old and venerable SwingWorker with something that is a bit more modern.

In the simplest use case, you do some kind of long-running work in your doInBackground() and then process the result of that work in done():

There’s a whole bunch of noise, so to speak, around the core of the logic – calling breadcrumb bar’s callback to get the leaf content for the newly set path, and then populating the folder based on the retrieved content. Let’s see how this code can look like with coroutines:

In this case, we have distilled the core of the logic flow to its essence – asynchronous loading of the leaf content, followed by updating the UI. Another option is to collapse the async / await block into a single withContext to change the execution context away from the UI thread:

Note that once you switch to coroutines, you also need a larger context for proper synchronization. In Swing, it means wrapping the entire listener with launch(Swing):

Now let’s take a look at something a bit more interactive – updating the UI on the ongoing progress of a long-running background task.

Let’s add a button and a label to a sample frame, and kick off a 5-second task that updates the UI every second on the progress using SwingWorker.process():

The first way to convert to coroutines would be with the help of channels:

Note the usage of Swing context that is passed to the launch() function and the wrapping of the emulated long-running task in the async lambda. Then, as long as that lambda keeps on sending content into the channel, the for loop iterates over the channel content on the UI thread, and then relinquishes the UI thread so that it is no longer blocked.

Now let’s make it a little bit more structured. While code samples found in documentation and tutorials run on the lighter side of things (including this article), real-life apps would have more complexity on both sides of async processing. We’re going to split this logic into two parts – background processing of data and updating the UI on the main thread. Background processing becomes a separate function that returns a data channel:

And UI code consumes the data posted to the channel and updates the relevant pieces:

Let’s make the background task cancellable so that the currently running operation can be safely canceled without subsequent erroneous UI updates. Background processing returns an object that has two parts – data channel and a job that can be canceled if needed:

And on the UI side of things, we keep track of the last job that was kicked off and cancel it:

Finally, we can move away from the existing concept of data communication “pipe” between the two parts, and start thinking in terms of passing a lambda to be invoked by the data producer when it has completed processing the next chunk of data. In this last example, the producer marks itself with the suspend keyword and uses the parent context so that cancellation is properly propagated:

And the UI side of things supplies a lambda that updates the relevant UI pieces:

If you want start playing with replacing SwingWorker with coroutines, add org.jetbrains.kotlinx:kotlinx-coroutines-swing and org.jetbrains.kotlinx:kotlinx-coroutines-jdk8 to your dependencies. The latest version for both of the modules is 0.24.0.

May 23rd, 2018

Hello Radiance

This week marks the beginning of a new phase for a bunch of my long-running open-source Swing projects. Some of them have started all the way back in 2005, and some have joined later on along the road. Over the years, they’ve been hosted on three sites (, and in three version control systems (cvs, svn, git). Approaching the 15th year mark (with a hiatus along the way), it’s clear that it’s time to revisit the fundamental structure of these projects and bring them into a more modern world.

Since these projects have been brought back to life in the last two years, the entire codebase has been revisited to clean up the cruft that has accumulated over time. Some of the explorations that I’ve embarked on have not went as well as I hoped they would be. That has been the fate of laf-plugin and laf-widget projects that aimed to bring common functionality across a variety of third-party look-and-feels, a field that is only seeing Substance and Synthetica as the two lone survivors.

Today I’m happy to announce the beginning of Project Radiance, the new umbrella brand that will unify and streamline the way Swing developers can integrate my libraries into their projects. At a high-level:

  • Radiance is a single project that provides a Gradle-based build that no longer relies on knowing exactly what to check out and where the dependent projects need to be located. It also uses proper third-party project dependencies to pull those at build time.
  • Starting from the very first release (planned for second half of 2018), Radiance will provide Maven artifacts for all core libraries – Trident (animation), Substance (look-and-feel), Flamingo (components), Ibis (SVG icons) and others.
  • The Kormorant sub-project is the first exploration into using Kotlin DSLs (domain-specific languages) for more declarative way of working with Swing UIs.
  • Flamingo components will only support Substance look-and-feel, no longer doing awkward and unnecessary tricks to try and support core and other third-party look-and-feels.

All the open bugs on existing GitHub projects have been migrated to be under Radiance. Once the migration of all the relevant documentation and older binaries to Radiance is complete in the next couple of weeks, those projects will be deleted from GitHub.

Starting from today, all new development such as bug fixes, feature work and documentation updates will only be done under Radiance. The versioning of all the projects will be unified going forward, resetting to 1.0. Some public APIs might move between sub-projects (going into Neon).

March 15th, 2018

Releases 2018.H1

Going with the biannual release cycle of my Swing projects, it’s time to do latest release batch.

Substance 8.0 (code-named Wyoming) is a major release that addresses technical debt accumulated in the API surface over the years and takes a major step towards enabling modern UI customizations for Swing applications. Full release notes and API listings are available, with the highlights being:

  • Unified API surface (Project Cerebrum)
  • Configurable title pane content (Project Visor)
  • Folded laf-plugin / laf-widget (Project Corpora)
  • Explicit instantiation of component and skin plugins
  • Switch to Material icons + icon pack support
  • Better support for fractional scaling factors

Flamingo 5.3 (code-named Liadan) has extracted the non-core functionality into two new projects:

  • Ibis has the code for using vector-based icons in Swing apps. It supports offline transcoding of SVG content into Java2D-powered classes, as well as dynamic display of SVG content at runtime (powered by the latest version of Apache Batik)
  • Spoonbill has the code for browsing SVN repositories with the JBreadcrumbBar component from the core Flamingo project. Future plans include extending this functionality to GitHub repositories as well.

If you’re in the business of writing Swing desktop applications, I’d love for you to take the latest releases of Substance and Flamingo for a spin. You can find the downloads in the /drop folders of the matching Github repositories. All of them require Java 8 to build and run.