Hello World - XDean/javafx-spring-boot GitHub Wiki
- Create FXML and Controller like normal JavaFX application.
<HBox xmlns:fx="http://javafx.com/fxml" fx:controller="xdean.jfx.spring.sample.SampleController">
<Button fx:id="button" text="sayHello" onAction="#sayHello"/>
</HBox>
public class SampleController {
@FXML
Button button;
@FXML
public void sayHello() {
}
}
- How to say hello? Provide a service!
public interface SampleService {
void sayHello();
}
@Service
public class SampleServiceImpl implements SampleService {
@Override
public void sayHello() {
System.out.println("Hello javafx-spring-boot!");
}
}
- Inject the service into the controller
public class SampleController {
@FXML
Button button;
@Inject
SampleService service;
@FXML
public void sayHello() {
service.sayHello();
}
}
- Let spring context manage the controller
@FxController(fxml = "/Sample.fxml")
public class SampleController {
...
}
- Create an entrance to start the application and show the fxml.
@SpringFxApplication // indicate this is a spring-fx application
public class SampleApp implements FxApplication { // FxApplication play the entrance role like javafx's Application
public static void main(String[] args) {
SpringApplication.run(SampleApp.class, args); // run as spring application
}
@Inject
FxmlResult<SampleController, Parent> fxml; // load and inject the controller
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(fxml.getRoot(), 400, 300);
stage.setTitle("Hello JavaFX-Spring-Boot");
stage.setScene(scene);
stage.show();
}
}
Now you can run the fx application as spring boot application!