https://github.com/yoep/spring-boot-starter-javafx
Spring Boot starter for JavaFX
https://github.com/yoep/spring-boot-starter-javafx
javafx spring spring-boot-starter
Last synced: 3 months ago
JSON representation
Spring Boot starter for JavaFX
- Host: GitHub
- URL: https://github.com/yoep/spring-boot-starter-javafx
- Owner: yoep
- License: apache-2.0
- Created: 2019-12-01T08:27:39.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-21T20:40:13.000Z (over 1 year ago)
- Last Synced: 2025-04-10T04:07:07.754Z (7 months ago)
- Topics: javafx, spring, spring-boot-starter
- Language: Java
- Homepage:
- Size: 501 KB
- Stars: 32
- Watchers: 5
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring Boot JavaFX starter
Spring Boot starter for easy integration with JavaFX.
This library provides easy integration with JavaFX as well as additional helpers for
loading & managing JavaFX views in Spring.
## Maven
The library is available in the maven central repository and can be used by adding:
_Spring Boot 3.X_
```xml
com.github.yoep
spring-boot-starter-javafx
2.0.0
```
_Spring Boot 2.X_
```xml
com.github.yoep
spring-boot-starter-javafx
1.0.12
```
## Requirements
### Version 1.0.0+
This is the legacy version of the library which requires Spring Boot 2+ and Java 8+.
- Spring Boot 2+
- Java 8+
### Version 2.0.0+
This is the new major version of the library which requires Spring Boot 3+ and Java 17+.
- Spring Boot 3+
- Java 17+
## Usage
Create a class which extends `SpringJavaFXApplication` and launch the JavaFX application from this class.
_main entry example_
```java
@SpringBootApplication
public class MySpringApplication extends SpringJavaFXApplication {
public static void main(String[] args) {
launch(MySpringApplication.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
super.start(primaryStage);
// YOUR CODE ON STARTUP HERE
}
}
```
Create a `ResourceBundle` bean which can be used by JavaFX within `.fxml` files.
_resource bundle example_
```java
@Configuration
public class LanguageConfig {
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("lang/example");
return messageSource;
}
}
```
### Borderless stage
You can create a borderless (undecorated) stage which is still resizable
and draggable by using the `BorderlessStage` class.
This functionality can also be achieved on the primary stage by using
the `BorderlessStageWrapper` as follows:
```java
@SpringBootApplication
public class MySpringApplication extends SpringJavaFXApplication {
@Override
public void start(Stage stage) throws Exception {
var myBorderlessStage = new BorderlessStageWrapper(stage);
super.start(stage);
// set the height of the header
// this height is used within the BorderlessStage
// to drag the window around
myBorderlessStage.setHeader(20);
// set the virtual border of the BorderlessStage
// this width is used to determine when the user can grab
// the non-existing border to resize the borderless stage
myBorderlessStage.setResizeBorder(2);
}
}
```
### View controllers
Use the following stereotype to define a view controller.
This is not required as the library will use any bean that is known within the
`ApplicationContext` to bind them to views.
```java
@ViewController
public class MyViewController {
}
```
### Loading views
There are 2 options when loading views, automatic controller selection through beans
or manually defining the controller that needs to be used by JavaFX.
- Automatically use a controller from the `ApplicationContext`.
```java
private class Example {
private ViewLoader viewLoader;
private void loadView() {
viewLoader.load("my-view-file.fxml");
}
}
```
- Define a controller which needs to be used in the view.
```java
private class Example {
private ViewLoader viewLoader;
private void loadView() {
ViewController controller = new ViewController();
viewLoader.load("my-view-file.fxml", controller);
}
}
```
## FAT jar packaging
It is **recommended to not use** the `spring-boot-maven-plugin` if you want to package the JavaFX application into a fat jar.
The reason behind this, is that the plugin will break JavaFX due to the JAR layout that is used by the plugin.
For creating fat jar packages, use the `maven-shade-plugin` instead.
```xml
org.apache.maven.plugins
maven-shade-plugin
false
package
shade
META-INF/spring.handlers
META-INF/spring.factories
META-INF/spring.schemas
${start-class}
```
## IntelliJ IDEA
IntelliJ adds by default the `javafx.base` and `javafx.graphics` to the modules of Java 9+.
This might be causing issues in Java 9 and above, as the `javafx.controls` and `javafx.fxml` are
missing from the modules causing an `IllegalAccessException` when trying to run the application.
Add the following options to the `VM Options` in the run configuration of IntelliJ to fix this issue.
-p "\lib" --add-modules javafx.controls,javafx.fxml,javafx.swing