0

This code has a TavbleView in it, the final column has buttons to act on the column, view & clear. the clear button should only be present or enabled (either would be fine) if the given row is in a known state. Here is the code to always display both buttons, it is coded right now that when the "canClear" property is false, no action is taken:

    private void addActionsToTable() {

        TableColumn<searchResults, Void> actionColumn = new TableColumn("Action");

        actionColumn.setCellFactory(col -> new TableCell<searchResults, Void>() {
            private final HBox container;

            {
                Button viewBtn = new Button("View");
                Button clearBtn = new Button("Clear");

                viewBtn.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        searchResults data = getTableView().getItems().get(getIndex());
                        gotoView(data.getLogNo());
                    }
                });

                clearBtn.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        searchResults data = getTableView().getItems().get(getIndex());

                        String logNo = data.getLogNo();
                        String serialNumber = data.getSerial();
                        
                        Boolean canClear = data.getCanClear();
                        if(canClear)
                        {
                            // Take action that has been cut for simplicity
                        }
                    }
                });

                container = new HBox(5, viewBtn, clearBtn);
            }

            @Override
            public void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    setGraphic(container);
                }
            }
        });

        SearchTable.getColumns().add(actionColumn);

        actionColumn.setPrefWidth(175);
    }

What need to happen so that the Clear button is either disabled or not displayed when data.getCanClear() is false?

1 Answer 1

2

Assuming your searchResults (sic) class has a BooleanProperty canClearProperty() method:

    actionColumn.setCellFactory(col -> new TableCell<searchResults, Void>() {
        private final HBox container;
        private final Button clearButton ;

        {
            Button viewBtn = new Button("View");
            clearBtn = new Button("Clear");

            viewBtn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    searchResults data = getTableView().getItems().get(getIndex());
                    gotoView(data.getLogNo());
                }
            });

            clearBtn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    searchResults data = getTableView().getItems().get(getIndex());

                    String logNo = data.getLogNo();
                    String serialNumber = data.getSerial();
                    
                    Boolean canClear = data.getCanClear();
                    if(canClear)
                    {
                        // Take action that has been cut for simplicity
                    }
                }
            });

            container = new HBox(5, viewBtn, clearBtn);
        }

        @Override
        public void updateItem(Void item, boolean empty) {
            clearButton.disableProperty().unbind();
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                clearButton.disableProperty().bind(
                    getTableView().getItems().get(getIndex())
                        .canClearProperty().not());
                setGraphic(container);
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.