根据 TableView javafx 的行更改的按钮图标
Button icon that changes according to the row of a TableView javafx
如果该行不为空,我想添加一列。在这个中,我想放置具有不同 ImageView 的按钮,以便能够转到添加或查看页面。
我正在尝试根据 TableView 中我的行的内容更改按钮的图标(此处如果 dateR (string) 为 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;
};
假设您的 ModelTab4
class 包括以下内容:
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());
现在单元实现中的 item
是 dateR
属性。代码的问题在于, 属性 更改的唯一方法是在按下按钮之后,并且没有通知返回到值已更改的单元格。因此,在 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;");
}
}
}
};
如果该行不为空,我想添加一列。在这个中,我想放置具有不同 ImageView 的按钮,以便能够转到添加或查看页面。 我正在尝试根据 TableView 中我的行的内容更改按钮的图标(此处如果 dateR (string) 为 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;
};
假设您的 ModelTab4
class 包括以下内容:
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());
现在单元实现中的 item
是 dateR
属性。代码的问题在于, 属性 更改的唯一方法是在按下按钮之后,并且没有通知返回到值已更改的单元格。因此,在 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;");
}
}
}
};