Search code examples
javajavafxtreetableviewjfoenix

Adding JFXButton into a JFXTreeTableView


I have tried to research about this, but could not find any examples. The examples I found were regarding normal TableView only. I could only create a JFXTreeTableView with an object and list out String. Any help would be appreciated! Thanks!

I need to put a button or basically any other object than a string into a TreeTableView.

Updated to make it clear on what I wanted it to look like, and the solution is below. enter image description here


Solution

  • With reference to this post, I was trying to add a Button(Specifically JFXButton) into a TreeTableView(Specifically JFXTreeTableView) How to add button in JavaFX table view

    However, the post only talks about TableView. After analyzing the codes I tried to modify the codes to work with TreeTableView and TreeTableCell instead.

    Using the sample codes from JFoenix and modifying it as seen in the codes snippets below, I could load a JFXButton into a JFXTreeTableView. (Also works with normal Button. Just replace the JFXButton to Button)

    JFXTreeTableColumn<User, String> settingsColumn = new JFXTreeTableColumn<>("Others");
            settingsColumn.setPrefWidth(100);
            Callback<TreeTableColumn<User, String>, TreeTableCell<User, String>> cellFactory
                    = //
                    new Callback<TreeTableColumn<User, String>, TreeTableCell<User, String>>() {
                        @Override
                        public TreeTableCell call(final TreeTableColumn<User, String> param) {
                            final TreeTableCell<User, String> cell = new TreeTableCell<User, String>() {
    
                                final JFXButton btn = new JFXButton("Just Do it");
    
                                @Override
                                public void updateItem(String item, boolean empty) {
                                    super.updateItem(item, empty);
                                    if (empty) {
                                        setGraphic(null);
                                        setText(null);
                                    } else {
                                        btn.setButtonType(JFXButton.ButtonType.RAISED);
                                        btn.setOnAction(event -> {
                                        //Button Action here
                                        });
                                        setGraphic(btn);
                                        setText(null);
                                    }
                                }
                            };
                            return cell;
                        }
                    };
    
    settingsColumn.setCellFactory(cellFactory);
    
    //Also remember to add the new column in
    treeView.getColumns().setAll(deptColumn, ageColumn, empColumn, settingsColumn);
    

    This is the end result: Image Example