我想在动作TableColumn中添加两个按钮,我已经阅读了这个How to add button in JavaFX table view和这个Add a button to a cells in a 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
    }
});


它只显示一个按钮:

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

我怎么解决这个问题?

最佳答案

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

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


结果:

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



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

关于java - 如何在TableView JavaFX的TableColumn中添加两个按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44696775/

10-17 03:12