【问题标题】:How to add two buttons in a TableColumn of TableView JavaFX如何在 TableView JavaFX 的 TableColumn 中添加两个按钮
【发布时间】:2017-11-25 13:52:40
【问题描述】:

我想在操作表列中添加两个按钮,我已经阅读了这个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 javafx


    【解决方案1】:

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

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

    结果:


    如果你有其他方法,我会很高兴的!

    【讨论】:

    • 如何使用 fxml?我想使用旋转的Label 将垂直标题添加到TableColumn。在 Java 中,我只使用 setGraphic() 和包含 LabelVBox
    • 找到它:&lt;graphic&gt;&lt;/graphic&gt;
    • ? 这很好,因为即使我也不知道这个,干得好@OrdinaryDraft
    猜你喜欢
    • 2022-01-20
    • 2015-09-17
    • 1970-01-01
    • 2021-12-04
    • 2014-04-07
    • 2020-05-07
    • 2019-12-21
    • 2016-10-27
    • 2017-12-16
    相关资源
    最近更新 更多