Hello World - XDean/javafx-spring-boot GitHub Wiki

Hello JavaFX-Spring-Boot!

source code

  1. Create FXML and Controller like normal JavaFX application.

Sample.fxml

<HBox xmlns:fx="http://javafx.com/fxml" fx:controller="xdean.jfx.spring.sample.SampleController">
  <Button fx:id="button" text="sayHello" onAction="#sayHello"/> 
</HBox>

SampleController.java

public class SampleController {
  @FXML
  Button button;
  
  @FXML
  public void sayHello() {
  }
}
  1. How to say hello? Provide a service!

SampleService.java

public interface SampleService {
  void sayHello();
}

SampleServiceImpl.java

@Service
public class SampleServiceImpl implements SampleService {
  @Override
  public void sayHello() {
    System.out.println("Hello javafx-spring-boot!");
  }
}
  1. Inject the service into the controller
public class SampleController {
  @FXML
  Button button;
  
  @Inject
  SampleService service;
  
  @FXML
  public void sayHello() {
    service.sayHello();
  }
}
  1. Let spring context manage the controller
@FxController(fxml = "/Sample.fxml")
public class SampleController {
  ...
}
  1. Create an entrance to start the application and show the fxml.

SampleApp.java

@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!

⚠️ **GitHub.com Fallback** ⚠️