- Class Spring
- Saved searches
- Use saved searches to filter your results more quickly
- License
- cosinus-code/spring-swing
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- A Simple Java Swing Spring Application
- Prerequisites
- Project Configuration
- Creating the JFrame
- Share Post
- Maximilian Schremser
Class Spring
An instance of the Spring class holds three properties that characterize its behavior: the minimum, preferred, and maximum values. Each of these properties may be involved in defining its fourth, value, property based on a series of rules.
An instance of the Spring class can be visualized as a mechanical spring that provides a corrective force as the spring is compressed or stretched away from its preferred value. This force is modelled as linear function of the distance from the preferred value, but with two different constants — one for the compressional force and one for the tensional one. Those constants are specified by the minimum and maximum values of the spring such that a spring at its minimum value produces an equal and opposite force to that which is created when it is at its maximum value. The difference between the preferred and minimum values, therefore, represents the ease with which the spring can be compressed and the difference between its maximum and preferred values, indicates the ease with which the Spring can be extended. See the sum(javax.swing.Spring, javax.swing.Spring) method for details.
By defining simple arithmetic operations on Spring s, the behavior of a collection of Spring s can be reduced to that of an ordinary (non-compound) Spring . We define the «+», «-«, max, and min operators on Spring s so that, in each case, the result is a Spring whose characteristics bear a useful mathematical relationship to its constituent springs.
A Spring can be treated as a pair of intervals with a single common point: the preferred value. The following rules define some of the arithmetic operators that can be applied to intervals ( [a, b] refers to the interval from a to b , where a
[a1, b1] + [a2, b2] = [a1 + a2, b1 + b2] -[a, b] = [-b, -a] max([a1, b1], [a2, b2]) = [max(a1, a2), max(b1, b2)]
[a1, b1, c1] + [a2, b2, c2] = [a1 + a2, b1 + b2, c1 + c2] -[a, b, c] = [-c, -b, -a] max([a1, b1, c1], [a2, b2, c2]) = [max(a1, a2), max(b1, b2), max(c1, c2)]
With both intervals and Spring s we can define «-» and min in terms of negation:
X - Y = X + (-Y) min(X, Y) = -max(-X, -Y)
For the static methods in this class that embody the arithmetic operators, we do not actually perform the operation in question as that would snapshot the values of the properties of the method’s arguments at the time the static method is called. Instead, the static methods create a new Spring instance containing references to the method’s arguments so that the characteristics of the new spring track the potentially changing characteristics of the springs from which it was made. This is a little like the idea of a lazy value in a functional language.
If you are implementing a SpringLayout you can find further information and examples in How to Use SpringLayout, a section in The Java Tutorial.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder .
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Spring Swing is a framework designed for building Spring-powered swing applications.
License
cosinus-code/spring-swing
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Spring Swing is a framework designed for building Spring-powered Swing applications.
It allows you to start a Swing application in full Spring context. It also allows injection of Spring beans into Swing objects created during application run:
import org.cosinus.swing.store.ApplicationStorage; import static org.cosinus.swing.context.ApplicationContextInjector.injectContext; public class SwingObject < @Autowired public ApplicationStorage applicationStorage; public SwingApplicationContextAware() < injectContext(this); > >
There are already Swing object rewritten to auto-inject the Spring context:
import org.cosinus.swing.form.Panel; import org.cosinus.swing.store.ApplicationStorage; public class MyPanel implements Panel < @Autowired public ApplicationStorage applicationStorage; >
Getting Started Using Spring Swing
Here is the Java code for starting a Spring Swing application:
package org.cosinus.swing.example; import org.cosinus.swing.boot.ApplicationFrame; import org.cosinus.swing.boot.SpringSwingApplication; import org.cosinus.swing.boot.SpringSwingBootApplication; import javax.swing.*; import java.awt.*; @SpringSwingBootApplication public class HelloWorld extends ApplicationFrame < @Override public void initComponents() < JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel("Hello World", SwingConstants.CENTER)); add(panel); > public static void main(String[] args) < SpringSwingApplication.run(HelloWorld.class, args); > >
parent> groupId>org.cosinuscode.swinggroupId> artifactId>spring-swing-boot-starter-parentartifactId> version>1.0.0version> parent> dependencies> dependency> groupId>org.cosinuscode.swinggroupId> artifactId>spring-swing-boot-starterartifactId> dependency> dependencies>
Spring Swing runs with java 11, so the compiler should be configured for java 11.
The spring-boot maven plugin can be used for packaging:
properties> . project.output>$/outputproject.output> application.name>spring-swing-exampleapplication.name> . properties> build> plugins> . plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> version>$ version> executions> execution> id>repackageid> configuration> executable>trueexecutable> outputDirectory>$ outputDirectory> finalName>$ finalName> configuration> execution> executions> plugin> plugins> build>
The jar can be executed from the $ folder:
Application Name and Icon
The application name (which is the main window title) and the application icon can be specified in the application properties file application.yml :
swing: application: name: Sping Swing Example icon: spring.png
The icon file default location is in the image resources folder.
To add Apache Log4j 2 to the application exclude the spring-boot-starter-logging and add spring-boot-starter-log4j2 :
dependency> groupId>org.cosinuscode.swinggroupId> artifactId>spring-swing-boot-starterartifactId> exclusions> exclusion> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-loggingartifactId> exclusion> exclusions> dependency> dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-log4j2artifactId> dependency>
Then add log4j2 file to configure the logging.
To translate the message, replace «Hello World» with translate(«hello.world») and add in the i18n resources folder the standard translations properties file messages_en_us.properties :
To change the language of the application, add the preferences.json file in the conf folder and specify the preferred language:
< "appearance": < "language": < "type": "language", "value": "fr" > > >
along with the translations file messages_fr_fr.properties :
To attach a menu to the application, add in the conf folder a menu.json file with the menu structure like the following:
< "menu": < "start.application": "control S", "quit.application": "control Q" >, "help": < "about.application": "F1" > >
To translate the specific keys (e.g. «start.application», «quit.application», «about.application»), add those in the translation files:
hello.world=Hello World start.application=Start quit.application=Quit about.application=About
To add a slash screen to the application, just provide the image in image resources folder and add it in SplashScreen-Image entry of jar manifest using the maven-jar-plugin :
properties> . splash.file.name>spring-splash.pngsplash.file.name> properties> build> plugins> . plugin> groupId>org.apache.maven.pluginsgroupId> artifactId>maven-jar-pluginartifactId> configuration> archive> manifestEntries> SplashScreen-Image>BOOT-INF/classes/image/$ SplashScreen-Image> manifestEntries> archive> configuration> plugin> plugins> build>
To add progress bar to the splash screen, we need to pass the -splash-progress argument when running the application:
$ ./spring-swing-example.jar -splash-progress
The progress bar can be customized using dedicated arguments:
$ ./spring-swing-example.jar \ -splash-progress \ -splash-progress-color=56,123,44 \ -splash-progress-y=245 \ -splash-progress-x=5
Distribute the Application
To simplify the run of the application, we can add the spring-swing-example.sh bash file with start command:
#! /bin/bash SOURCE="$ " while [ -h "$SOURCE" ]; do APPLICATION_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" done APPLICATION_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" "$JAVA_HOME/bin/java" \ -jar $APPLICATION_DIR/spring-swing-example.jar \ -splash-progress \ -splash-progress-color=56,123,44 \ -splash-progress-y=245 \ -splash-progress-x=5
and use maven-resources-plugin to copy all resources to the output folder:
build> plugins> . plugin> groupId>org.apache.maven.pluginsgroupId> artifactId>maven-resources-pluginartifactId> executions> execution> id>copy-run-resourcesid> phase>packagephase> goals> goal>copy-resourcesgoal> goals> configuration> outputDirectory>$ outputDirectory> resources> resource> directory>src/main/resourcesdirectory> resource> resources> configuration> execution> executions> plugin> plugins> build>
Now simply start the application by running the bash file from the output folder:
The application properties, preferences, translations and menu structure can now be updated directly from the output folder.
The Spring Swing is Open Source software released under version 2.0 of the Apache License.
About
Spring Swing is a framework designed for building Spring-powered swing applications.
A Simple Java Swing Spring Application
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:
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
Share Post
Maximilian Schremser
Survivalist, Developer, Naturalist