繁体   English   中英

如何在TableView JavaFX的TableColumn中添加两个按钮

[英]How to add two buttons in a TableColumn of TableView JavaFX

我想在动作TableColumn中添加两个按钮,我已经阅读了此如何在JavaFX表格视图中添加按钮,以及如何 在TableView(JAVAFX)的单元格中添加按钮,但是它们都使用setGraphic一个按钮,所以当我尝试时使用:

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
    }
});

它只显示一个按钮:

只需一个按钮

我怎么解决这个问题?

您可以使用HBox在一个组件的旁边添加一个组件,例如:

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

结果:

HBox中


如果您有其他选择,我会很高兴!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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