JavaFX:删除行后更新GridPane的最佳方法? [英] JavaFX: Best way to update GridPane after row deletion?

查看:382
本文介绍了JavaFX:删除行后更新GridPane的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GridPane(在ScrollPane中)作为带有用户条目的表。
每行包含一个用户名(Label),一个描述其状态的图标(ImageView)和两个按钮,其中第二个按钮用于删除用户条目。
下面的两张图片描绘了用户条目删除前后的滚动窗格

I am using a GridPane (inside a ScrollPane) as a table with user entries. Each row incorporates a username (Label), an icon depicting its state (ImageView) and two buttons, the second of which is used to delete the user entry. The two images below depict the scroll pane before and after the user entry deletion

更新:下面的代码示例:

UPDATE: Code sample below:

显而易见的问题是GridPane项目没有自动重新排列,删除的行仍然占用空间。
我是否必须提供手动重新安排的方法,还是有更方便的方法?

The obvious issue is that the GridPane items do not get automatically re-arranged and the deleted line still occupies space. Do I have to provide a method to manually make the re-arrangement or is there an easier way to go about it?

for (NonSudoUser nsuser : nonSudoUsers) { // the users I want to show in the gridpane
    // user row
    RowConstraints row = new RowConstraints(30);
    gridPaneNonSudoUsers.getRowConstraints().add(row);

    // code that adds username label, status icon, checkstatus button - irrelevant I think to our issue
    // adding delete user button now:
    Button btnDeleteUser = new Button("Delete User");
    btnDeleteUser.setId("btnDel"+username); // explained below
    // delete user in a seperate thread
    btnDeleteUser.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
        // confirmation dialog here - omitted for brevity               
            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)) // explaind below
                                        toErase.add(nodeToErase);
                                }
                                gridPaneNonSudoUsers.getChildren().removeAll(toErase);                                  
                                return null;
                            }
                        });
                    }
                });
                executorUserDeletion.submit(task);

检查天气元素id包含用户名的行是知道要删除的元素(在元素上)另外,我追加实际的用户名。所以包含例如testuser0条目的GridPane行由具有以下ID的元素组成:
lbltestuser0,imgtestuser0,btnCheckStatustestuser0,btnDeleteUsertestuser0。
(只是一个启发式,知道哪一行每次都要删除)
LinuxCommand是一个自定义类,在其构造函数中接受vararg String来运行相应的命令。

The line that checks weather an element id contains the username is to know which elements to delete (upon elements addition, I append the actual username. So the GridPane row containing e.g. testuser0 entry, is composed of elements with the following ids: lbltestuser0, imgtestuser0, btnCheckStatustestuser0, btnDeleteUsertestuser0. (just a heuristic to know which row to delete each time) LinuxCommand is a custom class that accepts vararg String in its constructor to run the respective command.

推荐答案

你可以使用setVisible(false)和setManaged(false)一起应用于调用删除操作的行元素,但它会导致网格线相互重叠并导致它有更深和更暗的网格线,如果有很多行删除。但它不会产生任何问题,除非你使用它作为GUI功能。

you can use setVisible(false) and setManaged(false) both together applied to row elements that delete action is invoked on, yet it will cause grid-lines overlapping each other and causing it to have darker and darker grid line if many rows get deleted. but it wont create any problem unless if you are using it as GUI feature.

这篇关于JavaFX:删除行后更新GridPane的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆