Reputation: 206
All the examples of spring and javafx implementation show the use of fxml, i would like to evade that.
@SpringBootApplication
@ComponentScan({"com.test.dao","com.test.service.impl","com.test.service","com.service.guiModules"})
public class Main extends Application {
private Workbench workbench;
private TestModule testModule = new TestModule();
public static void main(String[] args) {
//this commented just launches the spring app, but no gui
SpringApplication.run(Main.class, args);
//and when i use this it launches the gui, but spring cant manage beans
// Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene myScene = new Scene(initWorkbench());
primaryStage.setTitle("Simple WorkbenchFX Demo");
primaryStage.setScene(myScene);
primaryStage.setWidth(1000);
primaryStage.setHeight(700);
primaryStage.show();
primaryStage.centerOnScreen();
}
private Workbench initWorkbench() {
workbench = Workbench.builder(
testModule
).build();
return workbench;
}
then here is the GUI class
@Component
public class TestGui extends BorderPane {
// here would be gui and necesery gui logic, also here would i use my services impls
@Autowired
PersonService personService;
public TestGui() {
HBox hbox = new HBox();
Button btn = new Button();
Label lbl = new Label();
hbox.getChildren().addAll(btn,lbl);
setCenter(hbox);
btn.setOnAction(e->lbl.setText(personService.getSomething()));
}
here finally is class that returns gui to main classes workbench
public class TestModule extends WorkbenchModule {
//it just serves that TestGui class to the main classes workbench
public TestModule() {
super("Hello World", MaterialDesignIcon.HUMAN_HANDSUP);
}
@Override
public Node activate() {
return new TestGui();
}
}
I assume i must annotate TestGui class as component, so that later spring would know how to manage it. And i wont other classes its just Dao, POJO and service and its implementation.
In my AppConfig class i would like to tell spring to scan for beans in modules package
Normally when they use spring boot they use Fxml, so when they load files using FXML loader they do something like this. Is there anyway to rework this to help me with what i need ? i dont need or on plan using fxml, so i would like to just load javaclasses private final ResourceBundle resourceBundle; private final ApplicationContext context;
@Autowired
public AppConfig(ApplicationContext context, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
this.context = context;
}
public Parent load(String fxmlPath) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(context::getBean); //Spring now FXML Controller Factory
loader.setResources(resourceBundle);
loader.setLocation(getClass().getResource(fxmlPath));
return loader.load();
}
aside from spring and mysql dependencies in my pom i also have this and its what i am using to just load my gui classes to main stage
I understand that what i am asking is quite broad question, but i havent found one single example of spring boot and javafx without fxml.
Upvotes: 0
Views: 699
Reputation: 21
Annotate your WorkbenchModule
@Component
public class TestModule extends WorkbenchModule {
private Node node;
public TestModule() {
super("Test module", MaterialDesignIcon.DATABASE);
TextField id = new TextField();
Button load = new Button("Test");
node = new FlowPane(id, load);
}
@Override
public Node activate() {
return node;
}
}
*
@SpringBootApplication
@EnableScheduling
public class TestApplication extends Application {
private ConfigurableApplicationContext springContext;
// 1
@Autowired
private TestModule testModule;
public static void main(String[] args) {
launch(args);
}
// 3
@Override
public void init() throws Exception {
springContext = SpringApplication.run(OssApplication.class);
}
@Override
public void start(Stage primaryStage) {
// 4
Workbench workbench = springContext.getBean(Workbench.class);
Scene scene = new Scene(workbench);
primaryStage.setScene(scene);
primaryStage.setWidth(1000);
primaryStage.setHeight(700);
primaryStage.show();
primaryStage.centerOnScreen();
}
@Override
public void stop() {
springContext.close();
}
// 2
@Bean
public Workbench initWorkbench() {
Workbench workbench = Workbench.builder(
testModule
)
.build();
return workbench;
}
}
Upvotes: 1