0

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

zzdhxu
  • 379
  • 6
  • 22
  • how about reading a decent tutorial on fx basics? You need to learn the mechanics of how-to setup a tree/TableView (as you dont seem to know those, judging from _tried to set the column as type of button_) please see the info page of the javafx tag – kleopatra Jul 13 '18 at 08:45
  • Yes it is true I don't exactly understand that is why I came here for help. As to setting the column as type of button, that is one of the ways I tried. Some other examples that I followed were creating a new class with it extending a TableCell and setting the cell value factory but that dosen't really work too. It would be good if there is clearer examples on where to get more information. The generic codes provided work for me in putting an object of user information(SimpleStringProperty) into the table. Hope you can help. Thanks alot! – zzdhxu Jul 13 '18 at 09:38
  • Your question is unanswerable: it doesn't contain any information as to what exactly you want to achieve, nor what you tried nor how it went wrong. Please read https://stackoverflow.com/help/mcve and act accordingly. – kleopatra Jul 13 '18 at 10:22
  • Ok noted. Will add in code snippets and I'll try to explain in more detail later – zzdhxu Jul 13 '18 at 10:25
  • not a snippet please, what's needed is a mcve :) – kleopatra Jul 13 '18 at 10:51
  • I figured out how it work, but there was no explicit answer on it anywhere. Will be adding in the answer now – zzdhxu Jul 13 '18 at 11:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174940/discussion-between-zzdhxu-and-kleopatra). – zzdhxu Jul 13 '18 at 11:43

1 Answers1

3

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

zzdhxu
  • 379
  • 6
  • 22