根据TableView javafx的行而变化的按钮图标

问题描述 投票:0回答:1

如果行不为空,我想添加一列。在本教程中,我想放置具有不同ImageView的按钮,以便能够转到添加或查看页面。我正在尝试根据TableView中行的内容更改按钮的图标(此处,如果dateR(字符串)为null)我设法根据dateR值将其移至此类页面,但无法更改imageView ...谢谢

TableColumn<ModelTab4, Void> visualiserAjouter = new TableColumn<ModelTab4, Void>("Visualiser/Ajouter");

        Callback<TableColumn<ModelTab4, Void>, TableCell<ModelTab4, Void>> cellFactory1 = (final TableColumn<ModelTab4, Void> param) -> {
            final TableCell<ModelTab4, Void> cell = new TableCell<ModelTab4, Void>() {
                private String dateR;

                private final Button btn1 = new Button();

                {
                    btn1.setOnAction(click -> {
                        try {
                            ModelTab4 analyse = getTableView().getItems().get(getIndex());
                            dateR = analyse.getDateR();
                            setDate(dateR);
                            idAnalyse = analyse.getNum1();
                            if (dateR!=null){
                                mainApp.goConsultResult(idPerso, idPatient, idAnalyse);
                            }
                            else {
                                mainApp.goAjoutResult(idPerso, idPatient, idAnalyse);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                }
                public void setDate(String date) {
                    this.dateR = date;
                }
                public String getDate() {
                    return dateR;
                }
                public void updateItem(Void item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                    }
                    else {
                        if (getDate()!=null){
                            setGraphic(btn1);
                            ImageView icone1 = new ImageView("consult.png");
                            icone1.setFitHeight(20);
                            icone1.setFitWidth(20);
                            btn1.setGraphic(icone1);
                            setStyle("-fx-alignment : CENTER;");
                        }
                        else if (getDate()==null){
                            setGraphic(btn1);
                            ImageView icone2 = new ImageView("ajout.png");
                            icone2.setFitHeight(20);
                            icone2.setFitWidth(20);
                            btn1.setGraphic(icone2);
                            setStyle("-fx-alignment : CENTER;");
                        }
                    }
                }
            };

            return cell;
        };
java javafx tableview fxml
1个回答
0
投票

假设您的ModelTab4类包括以下内容:

public class ModelTab4 {

    private final StringProperty dateR = new SimpleStringProperty() ;

    public StringProperty dateRProperty() {
        return dateR ;
    }

    public final String getDateR() {
        return dateRProperty().get();
    }

    public final void setDateR(String dateR) {
        dateRProperty().set(dateR);
    }

    // other properties ...
}

您应该将visualiserAjouter列中的值设为dateR属性:

TableColumn<ModelTab4, String> visualiserAjouter = new TableColumn<>("Visualiser/Ajouter");
visualiserAjouter.setCellValueFactory(cellData -> cellData.getValue().dateRProperty());

现在,单元实现中的itemdateR属性。代码使用方式的问题在于,属性更改的唯一方法是按下按钮后,并且没有通知返回到单元格值已更改。因此,在updateItem()中,您正在检查dateR的值之前没有机会进行更改。使用上面的设置,您现在可以执行以下操作:

final TableCell<ModelTab4, Void> cell = new TableCell<ModelTab4, Void>() {

    private final Button btn1 = new Button();

    {
        btn1.setOnAction(click -> {
            try {
                ModelTab4 analyse = getTableView().getItems().get(getIndex());
                idAnalyse = analyse.getNum1();
                if (getItem()!=null){
                    mainApp.goConsultResult(idPerso, idPatient, idAnalyse);
                }
                else {
                    mainApp.goAjoutResult(idPerso, idPatient, idAnalyse);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    public void updateItem(String dateR, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        }
        else {
            if (dateR!=null){
                setGraphic(btn1);
                ImageView icone1 = new ImageView("consult.png");
                icone1.setFitHeight(20);
                icone1.setFitWidth(20);
                btn1.setGraphic(icone1);
                setStyle("-fx-alignment : CENTER;");
            }
            else if (dateR==null){
                setGraphic(btn1);
                ImageView icone2 = new ImageView("ajout.png");
                icone2.setFitHeight(20);
                icone2.setFitWidth(20);
                btn1.setGraphic(icone2);
                setStyle("-fx-alignment : CENTER;");
            }
        }
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.