1

I want to customize the button of the tableview, but it is empty when I get the show-hide-columns-button in the initialize method. There is a way to get the show-hide-columns-button.

    @FXML
    private TableView tableView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tableView.setTableMenuButtonVisible(true);
        final Node showHideColumnsButton = tableView
                .lookup(".show-hide-columns-button");
        System.out.println(showHideColumnsButton);
    }
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
laram
  • 41
  • 6
  • 2
    Calls to `lookup` are very likely to return `null` if called before the control is displayed in a window. The `initialize` method of an FXML controller is typically invoked before any of the nodes are added to a scene, let alone a window. – Slaw Jun 01 '19 at 07:41
  • @Slaw There are ways to solve this problem – laram Jun 01 '19 at 08:11
  • You could wrap the call in [`Platform.runLater()`](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-). – Zephyr Jun 01 '19 at 15:48

1 Answers1

0

As mentioned in the comments, a call to lookup(".show-hide-columns-button") returns null if the Scene is not shown yet. A simple solution:

tableView.sceneProperty().addListener((observable, oldScene, newScene) -> {
    if (newScene != null) {
        newScene.windowProperty().addListener((obs, oldWindow, newWindow) -> {
            if (newWindow != null) {
                newWindow.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> {
                    final Node showHideColumnsButton = tableView.lookup(".show-hide-columns-button");
                    // Customize your node here...
                    showHideColumnsButton.setStyle("-fx-background-color: red;");
                    event.consume();
                });
            }
        });
    }
});
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36