我可以使用 springboot-javafx-support 从初级阶段获取根 Pane 吗?
Can I get the root Pane from the primary stage using springboot-javafx-support?
我有一个主页调用 player.fxml
是这样的 BorderPane.Something
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="cn.will.controller.PlayerController"
fx:id="root"
prefHeight="670.0" prefWidth="1020.0" stylesheets="@../css/player.css">
<top>
<fx:include source="titleBar.fxml"/>
</top>
<left>
<fx:include source="userLeft.fxml"/>
</left>
<center>
<fx:include source="albumDetail.fxml"/>
</center>
<bottom>
<fx:include source="playBar.fxml"/>
</bottom>
</BorderPane>
在 Main.class 我启动应用程序。
@SpringBootApplication
public class Main extends AbstractJavaFxApplicationSupport implements ApplicationContextAware{
private static BorderPane root;
@Autowired
private ApplicationContext applicationContext;
public static void main(String[] args) {
launchApp(Main.class, PlayerView.class, new BlueprintSplashScreen(),args);
}
@Override
public void start(Stage stage) throws Exception {
super.start(stage);
stage.getIcons().add(new Image("img/music-icon32.png"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// ignore this. It's something I tried to meet my requirement.
//I try to use
//PlayerView view = applicationContext.getBean(PlayerView .class);
//root = (BorderPane) view.getView();
//but it throws NullPointerException dor applicationContext, it seems inject fail
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}
我想更改 BorderPane 中的中心内容 dynamically.I 确实需要获取此 BorderPane 以便我可以更改它。
顺便说一句,我也使用 Main.showView(xxx.class),但我只想改变 center.Not 整个阶段。
非常感谢先进。
你能post FXML 引用的控制器吗:fx:controller="cn.will.controller.PlayerController"
我认为如果您通过创建一个字段 @FXML BorderPane root;
.
在此处设置注释,您可以获得 BorderPane
我不熟悉 javafx-spring-support
库,但是您可以使用 Spring 和 JavaFX,而无需单独的库;我的 POM 中唯一的依赖项是 spring-boot-starter
(from Spring Boot 2.0.0.M7):
JAVA:
@SpringBootApplication
public class BootFXApplication {
public static void main(String[] args) {
Application.launch(BootFX.class, args);
}
@Bean
protected static Application bootFx() {
// I declare this bean this way so I can use
// the static instance stored when JavaFX
// calls the no-arg 'BootFX' constructor
// via reflection.
if (BootFX.instance == null) {
BootFX.instance = new BootFX();
}
return BootFX.instance;
}
public static class BootFX extends Application {
private static BootFX instance;
private ConfigurableApplicationContext applicationContext;
public BootFX() {
BootFX.instance = this;
}
@Override
public void init() throws Exception {
String[] args = !isNull(getParameters()) ?
getParameters().getRaw().toArray(new String[]{}) : new String[]{};
applicationContext = new SpringApplicationBuilder(BootFXApplication.class)
.run(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(Charset.forName("UTF-8"));
loader.setLocation(new ClassPathResource("fxml/main-pane.fxml").getURL());
// This is the secret-sauce, have JavaFX get controllers from Spring
loader.setControllerFactory(applicationContext::getBean);
primaryStage.setTitle("Spring Boot + JavaFX");
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
@Override
public void stop() throws Exception {
SpringApplication.exit(applicationContext);
}
}
@Bean
public MainPaneController getMainController() {
return new MainPaneController();
}
}
// You would have this in a separate class, and could mark as
// @Component instead of declaring the MainPaneCtrl bean above
class MainPaneController {
@FXML
private BorderPane rootPane;
public void initialize() {
System.out.println(rootPane.toString()); // Mess around with your root pane here, i.e. set bindings
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="rootPane" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.hypersonicgoat.bootfx.MainPaneController"/>
我借鉴了这位才华横溢的开发人员的概念;一定要查看他的博客以获取更多信息:http://www.greggbolinger.com/let-spring-be-your-javafx-controller-factory
我更熟悉 spring.By 使用 springboot-javafx-support 注释 @FXMLView 当我想要时,视图已经注册到 spring bean factory.So使用视图只是使用它。
使用@Autowire @Resources 将视图注入到您可以获取视图的位置并调用 getView() 方法来获取根窗格。
我有一个主页调用 player.fxml
是这样的 BorderPane.Something
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="cn.will.controller.PlayerController"
fx:id="root"
prefHeight="670.0" prefWidth="1020.0" stylesheets="@../css/player.css">
<top>
<fx:include source="titleBar.fxml"/>
</top>
<left>
<fx:include source="userLeft.fxml"/>
</left>
<center>
<fx:include source="albumDetail.fxml"/>
</center>
<bottom>
<fx:include source="playBar.fxml"/>
</bottom>
</BorderPane>
在 Main.class 我启动应用程序。
@SpringBootApplication
public class Main extends AbstractJavaFxApplicationSupport implements ApplicationContextAware{
private static BorderPane root;
@Autowired
private ApplicationContext applicationContext;
public static void main(String[] args) {
launchApp(Main.class, PlayerView.class, new BlueprintSplashScreen(),args);
}
@Override
public void start(Stage stage) throws Exception {
super.start(stage);
stage.getIcons().add(new Image("img/music-icon32.png"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// ignore this. It's something I tried to meet my requirement.
//I try to use
//PlayerView view = applicationContext.getBean(PlayerView .class);
//root = (BorderPane) view.getView();
//but it throws NullPointerException dor applicationContext, it seems inject fail
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}
我想更改 BorderPane 中的中心内容 dynamically.I 确实需要获取此 BorderPane 以便我可以更改它。 顺便说一句,我也使用 Main.showView(xxx.class),但我只想改变 center.Not 整个阶段。 非常感谢先进。
你能post FXML 引用的控制器吗:fx:controller="cn.will.controller.PlayerController"
我认为如果您通过创建一个字段 @FXML BorderPane root;
.
我不熟悉 javafx-spring-support
库,但是您可以使用 Spring 和 JavaFX,而无需单独的库;我的 POM 中唯一的依赖项是 spring-boot-starter
(from Spring Boot 2.0.0.M7):
JAVA:
@SpringBootApplication
public class BootFXApplication {
public static void main(String[] args) {
Application.launch(BootFX.class, args);
}
@Bean
protected static Application bootFx() {
// I declare this bean this way so I can use
// the static instance stored when JavaFX
// calls the no-arg 'BootFX' constructor
// via reflection.
if (BootFX.instance == null) {
BootFX.instance = new BootFX();
}
return BootFX.instance;
}
public static class BootFX extends Application {
private static BootFX instance;
private ConfigurableApplicationContext applicationContext;
public BootFX() {
BootFX.instance = this;
}
@Override
public void init() throws Exception {
String[] args = !isNull(getParameters()) ?
getParameters().getRaw().toArray(new String[]{}) : new String[]{};
applicationContext = new SpringApplicationBuilder(BootFXApplication.class)
.run(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(Charset.forName("UTF-8"));
loader.setLocation(new ClassPathResource("fxml/main-pane.fxml").getURL());
// This is the secret-sauce, have JavaFX get controllers from Spring
loader.setControllerFactory(applicationContext::getBean);
primaryStage.setTitle("Spring Boot + JavaFX");
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
@Override
public void stop() throws Exception {
SpringApplication.exit(applicationContext);
}
}
@Bean
public MainPaneController getMainController() {
return new MainPaneController();
}
}
// You would have this in a separate class, and could mark as
// @Component instead of declaring the MainPaneCtrl bean above
class MainPaneController {
@FXML
private BorderPane rootPane;
public void initialize() {
System.out.println(rootPane.toString()); // Mess around with your root pane here, i.e. set bindings
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="rootPane" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.hypersonicgoat.bootfx.MainPaneController"/>
我借鉴了这位才华横溢的开发人员的概念;一定要查看他的博客以获取更多信息:http://www.greggbolinger.com/let-spring-be-your-javafx-controller-factory
我更熟悉 spring.By 使用 springboot-javafx-support 注释 @FXMLView 当我想要时,视图已经注册到 spring bean factory.So使用视图只是使用它。 使用@Autowire @Resources 将视图注入到您可以获取视图的位置并调用 getView() 方法来获取根窗格。