JavaFX Multiple Controllers at the Same Time

て烟熏妆下的殇ゞ 提交于 2020-01-25 06:50:10

问题


I'm working on a JavaFX application and have run into the following problem while making the kind of "dashboard" of my app. The structure of the application is as follows:

This is the menu or "dashboard" which is kind of the main controller. The issue I'm running into is being able to send commands from the 'control panel' to the main view.

the fx:ids of appFrame and controlPanel are where these views are being displayed. How can I get these two panels (each with their own controller) to communicate?

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane fx:id="parent" prefHeight="600" prefWidth="1024.0" styleClass="parent" stylesheets="@stylesheet.css" xmlns="http://javafx.com/javafx/" xmlns:fx="http://javafx.com/fxml/" fx:controller="main.DashboardController">
    <VBox alignment="TOP_CENTER" prefHeight="600.0" prefWidth="300.0" styleClass="sidebar">
        <Pane prefHeight="75.0" prefWidth="300.0" style="-fx-background-color: #AED6F1#AED6F1;">
            <padding>
                <Insets left="10.0" />
            </padding>

                <ImageView fitHeight="75.0" fitWidth="236.0" layoutX="24.0" layoutY="8.0" pickOnBounds="true" preserveRatio="true">
            <Image url="main/logo.png" /></ImageView>
        </Pane>
        <HBox id="projects" alignment="CENTER_LEFT" layoutX="10.0" layoutY="132.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#handleViewChanged" prefHeight="40.0" prefWidth="300.0" styleClass="btns">
            <padding>
                <Insets left="10.0" />
            </padding>
            <cursor>
                <Cursor fx:constant="HAND" />
            </cursor>
            <Label text="Projects" textFill="#7c8184">
                <font>
                    <Font name="Calibri Light" size="15.0" />
                </font>
                <HBox.margin>
                    <Insets left="10.0" />
                </HBox.margin>
            </Label>
        </HBox>
        <HBox id="users" alignment="CENTER_LEFT" layoutX="10.0" layoutY="132.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#handleViewChanged" prefHeight="40.0" prefWidth="300.0" styleClass="btns">
            <padding>
                <Insets left="10.0" />
            </padding>
            <cursor>
                <Cursor fx:constant="HAND" />
            </cursor>
            <Label text="Users" textFill="#7c8184">
                <font>
                    <Font name="Calibri Light" size="15.0" />
                </font>
                <HBox.margin>
                    <Insets left="10.0" />
                </HBox.margin>
            </Label>
        </HBox>
        <HBox id="organizations" alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#handleViewChanged" prefHeight="40.0" prefWidth="300.0" styleClass="btns">
            <padding>
                <Insets left="10.0" />
            </padding>
            <cursor>
                <Cursor fx:constant="HAND" />
            </cursor>
            <Label text="Organizations" textFill="#7c8184">
                <font>
                    <Font name="Calibri Light" size="15.0" />
                </font>
                <HBox.margin>
                    <Insets left="10.0" />
                </HBox.margin>
            </Label>
        </HBox>
        <HBox id="templates" alignment="CENTER_LEFT" layoutX="10.0" layoutY="252.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#handleViewChanged" prefHeight="40.0" prefWidth="300.0" styleClass="btns">
            <padding>
                <Insets left="10.0" />
            </padding>
            <cursor>
                <Cursor fx:constant="HAND" />
            </cursor>
            <Label text="Templates" textFill="#7c8184">
                <font>
                    <Font name="Calibri Light" size="15.0" />
                </font>
                <HBox.margin>
                    <Insets left="10.0" />
                </HBox.margin>
            </Label>
        </HBox>
        <VBox prefHeight="10" prefWidth="300.0" styleClass="box" VBox.vgrow="ALWAYS">
            <VBox.margin>
                <Insets />
            </VBox.margin>
            <padding>
                <Insets left="10.0" top="10.0" />
            </padding>
        </VBox>
      <BorderPane fx:id="controlPane" prefHeight="150.0" prefWidth="300.0" styleClass="box" />

    </VBox>
    <VBox layoutX="300.0" prefHeight="600.0" prefWidth="724.0">
        <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="75.0" prefWidth="724.0" style="-fx-background-color: #DCDCDC;">
            <effect>
                <DropShadow blurType="ONE_PASS_BOX" color="#e8e4e4" height="10.0" radius="4.5" width="10.0" />
            </effect>
            <Label fx:id="titleText" layoutX="14.0" layoutY="24.0" text="Welcome" textFill="#7c8184">
                <font>
                    <Font size="18.0" />
                </font>
            </Label>
        </Pane>

        <HBox alignment="CENTER" />
        <BorderPane fx:id="appFrame" prefHeight="536.0" prefWidth="724.0">
            <center>
                <fx:include source="welcome.fxml" />
            </center>
        </BorderPane>
    </VBox>
</AnchorPane>

回答1:


MenuController - which will have the user object and load it in the UpdateUserController

public class MenuController implements Initializable {

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private JFXButton updateUser;

    private User user = new User("Joe","Rogan","username","password");


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        updateUser.setOnAction((ActionEvent event) -> {
            initializeUpdateController("Path of the fxml file", user , "title");
        });
    }
}

UpdateUserController

public class UpdateUserController implements Initializable {

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private TextField userNameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private JFXButton submit;

    private DatabaseHelper helper = new DatabaseHelper();


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        submit.setOnAction((ActionEvent event) -> {
            String userName = userNameField.getText();
            String password = passwordField.getText();
            String salt = helper.randomSaltString();
            password = salt + password + salt;
            password = sha256(password);

            helper.updateUserInDatabase(username,password);

        });
    }

    public void fillTextFields(User user) {
        userNameField.setText(user.getUsername());
        passwordField.setText(user.getPassword());
    }
}

This method will initialize the UpdateUserController with the fields that you want to pass.

public void initializeUpdateController(String path, User user, String title) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource(path));
        try {
            loader.load();
            UpdateUserController controller = loader.getController();
            controller.fillTextFields(user);
        } catch (IOException e) {
            System.out.println("Error init Update Controller " + e.getMessage());
        }
        loadAndShowStage(loader, title);
    }

 public static void loadAndShowStage(FXMLLoader loader, String title) {
        Parent parent = loader.getRoot();
        Stage stage = new Stage();
        stage.setScene(new Scene(parent));
        stage.setTitle(title);
        stage.show();
    }

For more help, check this link out. 1. Passing Parameters JavaFX FXML 2. Load FXML into a Pane(HBOX or VBOX)



来源:https://stackoverflow.com/questions/59652829/javafx-multiple-controllers-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!