JavaFX: Best way to update GridPane after row deletion?


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

GridPane before row (user entry) deletion

GridPane after row (user entry) deletion

UPDATE: Code sample below:

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);

    // 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>() {
        public void handle(ActionEvent event) {
        // confirmation dialog here - omitted for brevity               
            Task<Void> task = new Task<Void>() {
                public Void call() throws Exception {
                    String username = ((Button) event.getSource()).getId();
                    LinuxCommand lc = new LinuxCommand("userdel", "-r", username);
                    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>() {
                            public Void call() throws Exception {
                                ArrayList<Node> toErase = new ArrayList<Node>();
                                for (Node nodeToErase: gridPaneNonSudoUsers.getChildren()) {
                                    if (nodeToErase.getId().endsWith(username)) // explaind below
                                return null;

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.

10/16/2014 11:20:27 AM

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.

1/6/2016 6:03:33 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow