使用Spring的JavaFx自定义组件

时间:2016-03-19 16:29:12

标签: java spring javafx

我目前正在尝试在桌面应用程序的JavaFx中创建自定义组件,我想使用Spring DI。 我已经阅读了有关如何制作可扩展组件的内容,如果没有Spring,我可以根据此代码https://github.com/matrak/javafx-extend-fxml来完成。

但是当我用自定义SpringFXMLLoader替换FXMLLoader时,它无法加载组件。

在主窗口中,有一个按钮和一个编辑框,点击后我想创建一个具有给定名称的新自定义模块。但它说,自定义组件中的标签为空。

我做错了什么?

BasicModule.class

$cfg['Servers'][$i]['password'] = 'mypassword';

BasicModule.fxml

@org.springframework.stereotype.Controller
@DefaultProperty(value = "extension")
public class BasicModule extends BorderPane {
    public Label label;
    public AnchorPane extension;     
    public BasicModule() {
        super();
    }
    public ObservableList<Node> getExtension() {
        return extension.getChildren();
    }
}

CustomModule.class

<fx:root type="javafx.scene.layout.BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
     prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
     fx:controller="com.akos.fxmlTest.BasicModule">
  <top>
    <Label fx:id="label" text="Label Basic" BorderPane.alignment="CENTER"/>
  </top>
  <center>
    <AnchorPane fx:id="extension" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"/>
  </center>
</fx:root>

CustomModule.fxml

public class CustomModule extends BasicModule {
@FXML
  public Label customModuleLabel;    

  public void setText(String text) {
    customModuleLabel.setText(text);
  }
}

Main.class

<fx:root type="com.akos.fxmlTest.BasicModule" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
     fx:controller="com.akos.fxmlTest.CustomModule">
<Label fx:id="customModuleLabel" text="asdasdasdasd"/>
</fx:root>

main.fxml

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);

    context.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);

    SpringFxmlLoader loader = context.getBean(SpringFxmlLoader.class);
    Parent parent = (Parent) loader.load("/fxml/main.fxml");
    primaryStage.setScene(new Scene(parent, 1000, 600));
    primaryStage.setTitle("test");
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}
}

Controller.class

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.akos.fxmlTest.Controller">
<center>
  <VBox fx:id="itemList" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
     <BorderPane.margin>
        <Insets left="50.0" right="50.0" />
     </BorderPane.margin>
  </VBox>
</center>
 <left>
  <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
     <children>
        <Button fx:id="addButton" mnemonicParsing="false" onAction="#addNewModule" text="Button" />
        <TextField fx:id="moduleNameEdit" />
     </children>
  </VBox>
 </left>
</BorderPane>

AppConfiguration.class

@org.springframework.stereotype.Controller
public class Controller {

public VBox itemList;
public Button addButton;
public TextField moduleNameEdit;

public void addNewModule(ActionEvent actionEvent) {
    CustomModule customModule = new CustomModule();
    itemList.getChildren().add(customModule);
    customModule.setText(moduleNameEdit.getText());
}
}

对不起文字墙。

0 个答案:

没有答案