DAO的JavaFX动态编辑视图

时间:2018-12-14 12:01:05

标签: java javafx

我正在尝试尽可能动态地设计应用程序UI,因此无论何时创建新的DAO,都不需要太多代码即可在UI上显示DAO。 到目前为止,我已经创建了一个批注,用于在TableView中显示DAO。效果很好。

现在,我想创建一个用于编辑/创建DAO的新实例的动态视图。我的计划是只需要创建FXML,而内部的Components值会自动用DAO中的属性填充。

实现此目标的最佳做法是什么?

这里是一个例子:

国家/地区

public class Country extends DatabaseItem
{
    private String name;
    private String shortage;
    private String flagPath;

    private ImageView flag = null;

    public Country()
    {

    }
    [.. Getters/Setters ..]
}

目标

enter image description here

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<BorderPane fx:id="bPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="177.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <GridPane fx:id="gPane" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="294.0" minWidth="10.0" prefWidth="150.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="450.0" minWidth="10.0" prefWidth="450.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label fx:id="lblCountryName" text="Ländername:" />
            <Label fx:id="lblCountryShortage" text="Abkürzung:" GridPane.rowIndex="1" />
            <Label fx:id="lblCountryFlag" text="Flaggen-Symbol:" GridPane.rowIndex="2" />
            <TextField fx:id="txtCountryName" GridPane.columnIndex="1" />
            <TextField fx:id="txtCountryShortage" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <BorderPane fx:id="bPaneFlag" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
               <center>
                  <TextField fx:id="txtCountryFlag" BorderPane.alignment="CENTER">
                     <BorderPane.margin>
                        <Insets right="5.0" />
                     </BorderPane.margin>
                  </TextField>
               </center>
               <right>
                  <Button fx:id="btnCountryFlag" mnemonicParsing="false" onAction="#btnCountryFlagClicked" text="..." BorderPane.alignment="CENTER" />
               </right>
               <left>
                  <ImageView fx:id="imgViewCountryFlag" fitHeight="48.0" fitWidth="90.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
                     <BorderPane.margin>
                        <Insets right="5.0" />
                     </BorderPane.margin>
                  </ImageView>
               </left>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
            </BorderPane>
            <ButtonBar fx:id="btnBar" prefHeight="40.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3">
              <buttons>
                <Button fx:id="btnAbort" mnemonicParsing="false" onAction="#btnAbortClicked" text="Abbrechen" />
                  <Button fx:id="btnSave" mnemonicParsing="false" onAction="#btnSaveClicked" text="Speichern" />
              </buttons>
            </ButtonBar>
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </GridPane>
   </center>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

您可以在Countrey上添加一个接受fxml(模型)实例的控制器。它的一个非常简单的版本可能看起来像:

public class Controller {

    @FXML
    TextField txtCountryName, txtCountryShortage, txtCountryFlag;

    public void setModel (final Country countrey){
        txtCountryName.setText(countrey.getName());
        txtCountryShortage.setText(countrey.getID());
        txtCountryFlag.setText(countrey.getFlagPath());
    }
}

并在初始化时设置模型:

      FXMLLoader loader = new FXMLLoader();
      Parent root  =  loader.load(getClass().getResource("Main.fxml").openStream());
      Controller controller = (Controller)loader.getController();
      controller.setModel(new Country("Germany","DE","https://cdn3.iconfinder.com/data/icons/world-flags-square-vol-2/48/Germany-512.png"));