Today I have read about JSR296 the Swing App Framework by Karsten Lentzsch.

It inspired me to create a Java Swing Desktop Application. I wanted to use the Java Spring Framework for simplicity of property reading for the JFrame. Today I tried yo’s java-gradle generator. Yo is the CLI tool for running Yeoman generators. Yeoman helps you to kickstart new projects, read more about it here.

Prerequisites

  1. Install Gradle brew install gradle

  2. Install Npm brew install npm

  3. Install yo npm install -g yo

  4. Start yo and search for java-gradle yo

  5. Create a new project directory swing-spring mkdir swing-spring && cd swing-spring

  6. Create a new sample Java Project and follow the instructions yo java-gradle

Project Configuration

Having now a basic Java Project with Gradle integration I opened my Project From Existing Sources… in IntelliJ and added spring integration core as a compile dependency in the projects build.gradle file:

    compile "org.springframework.integration:spring-integration-core:4.2.1.RELEASE"

Creating the JFrame

I wanted to use annotation based spring initialization to avoid unnecessary resource configuration files.

The Main class is com.schremser.swing.spring.Launcher which initializes the Spring Beans using an AnnotationConfigApplicationContext. Add the Main class as mainClassName property to your build.gradle file to make it startable via gradle.

mainClassName = 'com.schremser.swing.spring.Launcher'

The Launcher class scans the SpringContext Configuration for Factory Beans to create.

The SpringContext uses a default.properties file to initialize set the title property of the MainFrame. The

The default.properties file has only one property title. title=Spring Main Frame.

The MainFrame initializes itself and shows up in the middle of the screen.

The whole project is hosted under GitHub


Maximilian Schremser

Survivalist, Developer, Naturalist