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
-
Install Gradle
brew install gradle
-
Install Npm
brew install npm
-
Install yo
npm install -g yo
-
Start yo and search for java-gradle
yo
-
Create a new project directory swing-spring
mkdir swing-spring && cd swing-spring
-
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:
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.
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