我正在使用此处使用的方法创建JavaFX
应用程序:http://www.oracle.com/technetwork/articles/java/javafx-productivity-2345000.html
我有两个看起来相似但行为不同的表单,所以我有两个控制器用于相同的FXML视图,,,我知道我可以手动创建控制器并执行:FXMLLoader.setController..
,但是用@Inject
注释的方式字段不会被注入。
我尝试使用名称空间传递控制器,但这不起作用。 我该怎么办?
编辑:一些代码
首先我有Guest FXML文件(它应该添加和更新来宾)
<?xml version="1.0" encoding="UTF-8"?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="pnlMain" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="302.0" prefWidth="337.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ro.terminal.controller.guest.AddGuestController">
<children>
<HBox layoutX="-33.0" layoutY="63.0" prefHeight="100.0" prefWidth="200.0" AnchorPane.bottomAnchor="80.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<children>
<VBox fx:id="boxDetailsHolder" prefHeight="450.0" prefWidth="390.0" spacing="5.0">
<children>
<Label prefHeight="19.0" prefWidth="178.0" style="-fx-font-size: 13;" text="%FXML.GUEST_DETAILS" textFill="#626973">
</Label>
<StackPane prefHeight="35.0">
<children>
<HBox alignment="CENTER_LEFT" maxHeight="1.7976931348623157E308" prefWidth="614.0">
<children>
<ComboBox fx:id="cmbSalutation" maxHeight="1.7976931348623157E308" maxWidth="106.0" minWidth="106.0" prefHeight="35.0" prefWidth="106.0" styleClass="combo-box-common">
<HBox.margin>
<Insets />
</HBox.margin>
<padding>
<Insets left="25.0" />
</padding>
</ComboBox>
<TextField fx:id="txtGuestName" maxHeight="1.7976931348623157E308" prefHeight="35.0" prefWidth="311.0" promptText="%General.NAME" styleClass="text-field-common">
<HBox.margin>
<Insets left="5.0" right="10.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<MaterialDesignIconView fill="#6e7175" glyphName="ACCOUNT" size="25" StackPane.alignment="CENTER_LEFT">
<StackPane.margin>
<Insets left="5.0" />
</StackPane.margin>
</MaterialDesignIconView>
</children>
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</StackPane>
<StackPane prefHeight="35.0">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="35.0" prefWidth="664.0">
<children>
<ComboBox fx:id="cmbCountry" maxHeight="1.7976931348623157E308" maxWidth="106.0" minWidth="106.0" prefWidth="106.0" styleClass="combo-box-common">
<HBox.margin>
<Insets />
</HBox.margin>
<padding>
<Insets left="25.0" />
</padding>
</ComboBox>
<StackPane HBox.hgrow="ALWAYS">
<children>
<TextField fx:id="txtPhone" maxHeight="1.7976931348623157E308" prefWidth="153.0" promptText="%General.PHONE_NUM" styleClass="text-field-common">
<padding>
<Insets left="6.0" right="50.0" />
</padding>
<StackPane.margin>
<Insets left="5.0" />
</StackPane.margin>
</TextField>
<Button fx:id="btnDuplicatePhone" mnemonicParsing="false" styleClass="btn-third-level" visible="false" StackPane.alignment="CENTER_RIGHT">
<graphic>
<MaterialDesignIconView glyphName="ACCOUNT_MULTIPLE_OUTLINE" size="20" />
</graphic>
<StackPane.margin>
<Insets right="5.0" />
</StackPane.margin>
</Button>
</children>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</StackPane>
</children>
</HBox>
<MaterialDesignIconView fill="#6e7175" glyphName="CELLPHONE" size="20" StackPane.alignment="CENTER_LEFT">
<StackPane.margin>
<Insets left="7.0" />
</StackPane.margin>
</MaterialDesignIconView>
</children>
</StackPane>
<StackPane prefHeight="35.0">
<children>
<TextField fx:id="txtEmail" maxHeight="1.7976931348623157E308" prefWidth="264.0" promptText="%General.EMAIL" styleClass="text-field-common">
<StackPane.margin>
<Insets right="10.0" />
</StackPane.margin>
<padding>
<Insets left="35.0" right="50.0" />
</padding>
</TextField>
<MaterialDesignIconView fill="#6e7175" glyphName="EMAIL" size="20" StackPane.alignment="CENTER_LEFT">
<StackPane.margin>
<Insets left="7.0" />
</StackPane.margin>
</MaterialDesignIconView>
<Button fx:id="btnDuplicateEmail" mnemonicParsing="false" styleClass="btn-third-level" visible="false" StackPane.alignment="CENTER_RIGHT">
<graphic>
<MaterialDesignIconView glyphName="ACCOUNT_MULTIPLE_OUTLINE" size="20" />
</graphic>
<StackPane.margin>
<Insets right="15.0" />
</StackPane.margin>
</Button>
</children>
</StackPane>
</children>
</VBox>
</children>
</HBox>
<Separator layoutY="558.0" prefWidth="200.0" AnchorPane.bottomAnchor="67.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" />
<HBox alignment="CENTER_RIGHT" layoutX="711.0" layoutY="558.0" prefHeight="70.0" prefWidth="337.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Button fx:id="btnAddGuest" graphicTextGap="5.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="120.0" styleClass="btn-secondary" textFill="#626973">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<graphic>
<FontAwesomeIconView fill="#626973" glyphName="PLUS" size="15" />
</graphic>
</Button>
</children>
</HBox>
</children>
</AnchorPane>
AddGuestController:
public class AddGuestController extends AbstractController {
/**
*
*/
@FXML private AnchorPane pnlMain;
/**
*
*/
@FXML private TextField txtGuestName, txtEmail, txtPhone;
@FXML private ComboBox<Country> cmbCountry;
@FXML private ComboBox<Salutation> cmbSalutation;
@FXML private Button btnAddGuest;
@Inject private Guest guest;
@Override
public void initialize(URL location, ResourceBundle resources) {
initGuest(); // this will be called on guest add only
setGuest(guest); // this will be called on guest update only
initMainActions();
}
/**
* @param guest
*/
private void setGuest(Guest guest) {
this.guest = guest;
txtGuestName.setText(this.guest.getGuestName());
txtEmail.setText(this.guest.getEmailAddress());
txtPhone.setText(this.guest.getPhoneNumber());
cmbCountry.getSelectionModel().select(this.guest.getCountry());
cmbSalutation.getSelectionModel().select(this.guest.getSalutation());
}
/**
*
*/
private void initMainActions() {
// use this on adding a guest
btnAddGuest.setOnAction(e-> {
if (localValidation()){
GuestCommonModel.addGuest(guest);
}
});
// use this on updating a guest
// btnAddGuest.setOnAction(e -> {
// if (localValidation()){
// GuestCommonModel.updateGuest(guest);
// }
// });
}
/**
*
*/
private void initGuest() {
if (guest != null)
return;
guest = new Guest();
guest.setPersonId(IDUtils.newID());
guest.setContactId(IDUtils.newID());
guest.setCityId(TerminalModule.instance().getVenue().getCityId());
guest.setAcceptEmail(true);
guest.setAcceptSms(true);
}
}