简体   繁体   中英

How to add two buttons in a TableColumn of TableView JavaFX

I want to add two button in action TableColumn, i already read this How to add button in JavaFX table view and this Add a button to a cells in a TableView (JAVAFX) but both of them use one button in setGraphic , so when i try to use :

actionFld.setCellFactory(param -> new TableCell<Patient, Patient>() {
    private final JFXButton editButton = new JFXButton("edit");
    private final JFXButton deleteButton = new JFXButton("delete");

    @Override
    protected void updateItem(Patient patient, boolean empty) {
        super.updateItem(patient, empty);

        if (patient == null) {
            setGraphic(null);
            return;
        }

        deleteButton.setOnAction(event -> {
            Patient getPatient = getTableView().getItems().get(getIndex());
            System.out.println(getPatient.getNom() + "   " + getPatient.getPrenom());
        });

        editButton.setOnAction(event -> {
            Patient getPatient = getTableView().getItems().get(getIndex());
            System.out.println(getPatient.getNom() + "   " + getPatient.getPrenom());
        });

        setGraphic(deleteButton);//<<<---------------add button 1
        setGraphic(editButton);//<<------------------add button 2
    }
});

it show me just one button :

只需一个按钮

How can i solve this problem?

You can use HBox to add your component one beside the other for example :

HBox pane = new HBox(deleteButton, editButton);
setGraphic(pane);

result:

HBox中


If you have another way, i will be happy for it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM