5

I know that it is possible to create a column in TableView filled with buttons thanks to jewelsea.

But I want to know if it's possible to define it directly in the FXML.

As an example, with other types one do:

Class Person:

private final SimpleStringProperty birthDate = new SimpleStringProperty("");

Then in the FXML:

    <TableView fx:id="table" layoutY="50.0" prefHeight="350.0" prefWidth="600.0">
      <columns>
        <TableColumn prefWidth="79.5" text="date of birth">
            <cellValueFactory>
               <PropertyValueFactory property="birthDate" />
            </cellValueFactory>
        </TableColumn>
      </columns>
    </TableView>

And one can add this element with:

@FXML private TableView<Person> table;
//...
table.getItems().add("12/02/1452");

How to achieve the same with Buttons?

Community
  • 1
  • 1
Mansueli
  • 6,223
  • 8
  • 33
  • 57

2 Answers2

0

No it's not possible you have init it with code

class ButtonListCell extends ListCell<MyObject> {
    @Override
    public void updateItem(MyObject obj, boolean empty) {
        super.updateItem(obj, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            setText(obj.toString());

            Button butt = new Button();
            butt.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("clicked");
                }                            
            });
            setGraphic(butt);
        }
    }
}

listview.setCellFactory(new Callback<ListView<MyObject>, ListCell>() {
    @Override
    public ListCell call(ListView<MyObject> param) {
        return new ButtonListCell();
    }

});
Ash
  • 71
  • 9
-1

It's not possible to add buttons to table column in FXML since cell factory is required.

You can do it with Java code as you jewelsea did.

Zaid Sultan
  • 78
  • 1
  • 9