0

好的,这是一个艰难的,现在已经让我发疯了几天......

我有两个阶段: 用户管理阶段 具有以下元素层次结构 的用户管理阶段(控制器:UsersAdminController.java)用户管理阶段的元素层次结构 和用户添加阶段:(控制器:AddUserController.java) 用户添加阶段

作为用户管理阶段上部锚窗格的滚动窗格的子项(请参阅元素层次结构),我在 UsersAdminController 的初始化期间添加了一个 GridPane,旨在列出系统的用户,如此处所述 我通过用户添加用户时在添加屏幕时,我尝试通过获取对 UsersAdminController 的引用然后调用更新其内容的方法来更新用户管理阶段的 GridPane。但是,无法更新 GridPane。(我似乎无法对滚动窗格和网格窗格执行任何与 gui 相关的操作,尽管我设法获得了对相关控制器的引用。

代码:这是通过 UsersAdminController 的初始化方法(正确)初始化网格窗格的方式:

public void initialize(URL location, ResourceBundle resources) {
    gridPaneNonSudoUsers = new GridPane();
    gridPaneNonSudoUsers.setId("gridPaneNonSudoUsers");
    // setting column widths for the grid pane
    gridPaneNonSudoUsers.getColumnConstraints().add(new ColumnConstraints(145));
    gridPaneNonSudoUsers.getColumnConstraints().add(new ColumnConstraints(85));
    gridPaneNonSudoUsers.getColumnConstraints().add(new ColumnConstraints(210));
    gridPaneNonSudoUsers.getColumnConstraints().add(new ColumnConstraints(150));

    srcPaneUsers.setContent(gridPaneNonSudoUsers);
    gridPaneNonSudoUsers.setAlignment(Pos.CENTER);

    displayNonSudoUsers(getSystemNonSudoUsers());
}

这是填充 GridPane 的 displayNonSudoUsers

公共无效显示非SudoUsers(ArrayList nonSudoUsers){

    // adding some space between lines
    gridPaneNonSudoUsers.setVgap(15);

    Label lblHeaderUsername = new Label("Username");
    lblHeaderUsername.setPadding(new Insets(0, 0, 0, 20));
    lblHeaderUsername.setId("lblHeaderUsername");
    Label lblHeaderIsProtected = new Label("Προστασία");
    lblHeaderIsProtected.setId("lblHeaderIsProtected");
    GridPane.setHalignment(lblHeaderUsername, HPos.LEFT);
    GridPane.setHalignment(lblHeaderIsProtected, HPos.CENTER);

    // adding header elements to GridPane
    GridPane.setColumnIndex(lblHeaderUsername, 0);
    GridPane.setRowIndex(lblHeaderUsername, 0);
    GridPane.setColumnIndex(lblHeaderIsProtected, 1);
    GridPane.setRowIndex(lblHeaderIsProtected, 0);
    gridPaneNonSudoUsers.getChildren().addAll(lblHeaderUsername,
            lblHeaderIsProtected);

    // adding users
    int i = 1; // starting from 2nd row given that 1st row belongs to
                // headers
    for (NonSudoUser nsuser : nonSudoUsers) {

        /**** Label ****/
        String username = nsuser.getUsername();
        Label lblUsername = new Label(username);
        lblUsername.setPadding(new Insets(5, 0, 5, 20));
        lblUsername.setId("lbl" + username);
        GridPane.setHalignment(lblUsername, HPos.LEFT);

        /**** Image ****/
        boolean isProtected = nsuser.isProtected();
        ImageView imgIsProtected = new ImageView(
                isProtected ? "/resources/media/notOK.png"
                        : "/resources/media/ok.png");
        imgIsProtected.setFitHeight(20);
        imgIsProtected.setFitWidth(20);
        imgIsProtected.setId("img" + username);
        GridPane.setHalignment(imgIsProtected, HPos.CENTER);

        /**** Button for protection ****/
        Button btnSetProtected = new Button("Ενεργοποίηση Προστασίας");
        btnSetProtected.setId("btnProt" + username);
        btnSetProtected.setDisable(!isProtected);
        GridPane.setHalignment(btnSetProtected, HPos.CENTER);

        /**** Button for deletion ****/
        Button btnDeleteUser = new Button("Διαγραφή Χρήστη");
        btnDeleteUser.setId("btnDel" + username);
        GridPane.setHalignment(btnDeleteUser, HPos.CENTER);
        // delete user in a seperate thread
        btnDeleteUser.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Action response = Dialogs
                        .create()
                        .owner(root)
                        .title("ΔΙΑΓΡΑΦΗ ΧΡΗΣΤΗ")
                        .masthead(
                                "ΠΡΟΣΟΧΗ! ΠΡΟΚΕΙΤΑΙ ΝΑ ΔΙΑΓΡΑΨΕΤΕ ΤΟ ΧΡΗΣΤΗ: "
                                        + username)
                        .message("ΕΙΣΑΣΤΕ ΣΙΓΟΥΡΟΙ?")
                        .actions(Dialog.ACTION_OK, Dialog.ACTION_CANCEL)
                        .showConfirm();
                if (response == Dialog.ACTION_CANCEL)
                    return;
                Task<Void> task = new Task<Void>() {
                    @Override
                    public Void call() throws Exception {
                        String username = ((Button) event.getSource())
                                .getId();
                        LinuxCommand lc = new LinuxCommand("userdel", "-r",
                                username);
                        lc.execute();
                        return null;
                    }
                };
                task.setOnFailed(new EventHandler<WorkerStateEvent>() {
                    public void handle(WorkerStateEvent t) {
                        System.out.println("FAILURE deleting  " + username);
                    }
                });
                task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
                    public void handle(WorkerStateEvent t) {
                        Platform.runLater(new Task<Void>() {
                            @Override
                            public Void call() throws Exception {
                                ArrayList<Node> toErase = new ArrayList<Node>();
                                for (Node nodeToErase : gridPaneNonSudoUsers
                                        .getChildren()) {
                                    if (nodeToErase.getId().endsWith(
                                            username))
                                        toErase.add(nodeToErase);
                                }
                                gridPaneNonSudoUsers.getChildren()
                                        .removeAll(toErase);
                                // rearrangeGridPane(); - TBD
                                return null;
                            }
                        });
                    }
                });
                executorUserDeletion.submit(task);
            }
        });

        // adding user-related gui items
        GridPane.setColumnIndex(lblUsername, 0);
        GridPane.setRowIndex(lblUsername, i);

        GridPane.setColumnIndex(imgIsProtected, 1);
        GridPane.setRowIndex(imgIsProtected, i);

        GridPane.setColumnIndex(btnSetProtected, 2);
        GridPane.setRowIndex(btnSetProtected, i);

        GridPane.setColumnIndex(btnDeleteUser, 3);
        GridPane.setRowIndex(btnDeleteUser, i);

        gridPaneNonSudoUsers.getChildren().addAll(lblUsername,
                imgIsProtected, btnSetProtected, btnDeleteUser);
        i++;
    }
}

这是在用户添加阶段的 ADD USER BUTTON 的操作上调用的方法

    public void createUser() throws UserCreationException {

        String username, password1, password2;
        username = txtUsername.getText();
        password1 = txtPassword1.getText();
        password2 = txtPassword2.getText();
        LinuxCommand lc = new LinuxCommand(System.getProperty("user.dir")+"/adduser.sh", username, password2);
    Task<Void> task = new Task<Void>() {
        @Override
        public Void call() throws Exception {
            lc.execute();
            return null;
        }
    };
    task.setOnFailed(new EventHandler<WorkerStateEvent>() {
        public void handle(WorkerStateEvent t) {
            System.out.println("FAILURE creating  " + username);
            System.out.println("exit code: " + lc.getExitCode());
        }
    });
    task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t) {
            if (lc.getExitCode() != 0)
                // show dialog
                return;
            System.out.println("Successfully created new user:  " + username + " With exit code: " + lc.getExitCode());
            Platform.runLater(new Task<Void>() {
                @Override
                public Void call() throws Exception {                       
                    uac.addUser();          
                    return null;

                }
            });
        }
    });

    executorUserCreation.submit(task);

其中 uac 是对 UserAdministrationController.java 的引用,adduser 是尝试通过再次调用 displayNonSudoUsers() 来更新 GridPane 的方法。我还尝试在用户管理阶段的滚动窗格/网格窗格上做各种事情(例如删除标签等),但是 NADA!

谢谢。

编辑:这就是我在 AddUserController.java 的初始化方法中获取对用户管理控制器的引用的方式(这可能是正确的,因为例如我设法 sysout 它的一些元素的 id 等)

public void initialize(URL location, ResourceBundle resources) {
    // need to access UsersAdminController to update users list 
    // when adding user
    URL loc = getClass().getResource("/resources/fxml/UsersAdminPane.fxml"); // the UsersAdmin fxml 
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(loc);
    fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
    try {
        Parent root = (Parent) fxmlLoader.load(loc.openStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    uac = (UsersAdminController)fxmlLoader.getController();
}
4

1 回答 1

1

在这段代码中:

public void initialize(URL location, ResourceBundle resources) {
    // need to access UsersAdminController to update users list 
    // when adding user
    URL loc = getClass().getResource("/resources/fxml/UsersAdminPane.fxml"); // the UsersAdmin fxml 
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(loc);
    fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
    try {
        Parent root = (Parent) fxmlLoader.load(loc.openStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    uac = (UsersAdminController)fxmlLoader.getController();
}

您加载 的新副本UsersAdminPane.fxml,获取对其控制器的引用...然后丢弃视图 ( root) 并且永远不要对它做任何事情。由于该视图永远不会显示,因此调用控制器上的方法永远不会产生您将看到的任何更新。

您需要获取与现有视图关联的控制器的引用,并在其上调用方法,而不是创建新控制器。假设您从 加载 AddUser fxml文件,UsersAdminController您可以AddUserController通过FXMLLoader在.UsersAdminControllersetUsersAdminController(...)AddUserController

于 2014-10-20T17:51:37.383 回答