gpt4 book ai didi

java - 一列添加两个按钮并获取点击行值javafx

转载 作者:行者123 更新时间:2023-11-30 06:29:05 25 4
gpt4 key购买 nike

我正在尝试在 javafx 的 TableView 中的一列中实现两个按钮。我能够在一列中实现一个按钮,但无法在一列中添加两个按钮。 我从这个链接得到了一些想法

How to add button in JavaFX table view

Add a button to a cells in a TableView (JAVAFX)

How to add two buttons in a TableColumn of TableView JavaFX

我已经使用了上面链接中的想法。但是,该代码对我不起作用。

    TableColumn<Student, String> firstCol = new TableColumn<Student, String>("ID");
TableColumn<Student, String> secondCol = new TableColumn<Student, String>("Name");
TableColumn<Student, String> thirdCol = new TableColumn<Student, String>("Quiz Mark");
TableColumn<Student, String> forthCol = new TableColumn<Student, String>("A1");
TableColumn<Student, String> fifthCol = new TableColumn<Student, String>("A2");
TableColumn<Student, String> sixthCol = new TableColumn<Student, String>("A3");
TableColumn<Student, String> sevenCol = new TableColumn<Student, String>("Exam");
TableColumn<Student, String> eightCol = new TableColumn<Student, String>("Result");
TableColumn<Student, String> nineCol = new TableColumn<Student, String>("Grade");
TableColumn<Student, Student> tenthCol = new TableColumn<Student, Student>("Action");


firstCol.setCellValueFactory(new PropertyValueFactory<>("Id"));
secondCol.setCellValueFactory(new PropertyValueFactory<>("Name"));
thirdCol.setCellValueFactory(new PropertyValueFactory<>("QuizMark"));
forthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment1Mark"));
fifthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment2Mark"));
sixthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment3Mark"));
sevenCol.setCellValueFactory(new PropertyValueFactory<>("ExamMark"));
eightCol.setCellValueFactory(new PropertyValueFactory<>("Result"));
nineCol.setCellValueFactory(new PropertyValueFactory<>("Grade"));
tenthCol.setCellFactory(param -> new TableCell<Student, Student>() {
private final Button editButton = new Button("edit");
private final Button deleteButton = new Button("delete");
HBox pane = new HBox(deleteButton, editButton);

@Override
protected void updateItem(Student patient, boolean empty) {
super.updateItem(patient, empty);

if (patient == null) {
setGraphic(null);
return;
}

deleteButton.setOnAction(event -> {
Student getPatient = getTableView().getItems().get(getIndex());
System.out.println(getPatient.getId() + " " + getPatient.getName());
});

editButton.setOnAction(event -> {
Student getPatient = getTableView().getItems().get(getIndex());
});
pane.getChildren().addAll(deleteButton,editButton);
setGraphic(pane);
}
});

image to describe the text

其中,操作列按钮不会出现。我哪里做错了。请任何人告诉我。

最佳答案

缺少 cellValueFactory tenthColumn 中的单元格项目始终 null 。使用 updateItem 的第二个参数判断该行是否为空的方法。

为了表明这一点,我将列的类型更改为 TableColumn<Student, Void> .

进一步添加 HBox 的子项每次更改项目时都不需要再次更新 onAction处理程序,自 getIndex()Button 时进行评估被点击。

您可以将代码更改为:

TableColumn<Student, Void> tenthCol = new TableColumn<>("Action");

...

tenthCol.setCellFactory(param -> new TableCell<Student, Void>() {
private final Button editButton = new Button("edit");
private final Button deleteButton = new Button("delete");
private final HBox pane = new HBox(deleteButton, editButton);

{
deleteButton.setOnAction(event -> {
Student getPatient = getTableView().getItems().get(getIndex());
System.out.println(getPatient.getId() + " " + getPatient.getName());
});

editButton.setOnAction(event -> {
Student getPatient = getTableView().getItems().get(getIndex());
});
}

@Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);

setGraphic(empty ? null : pane);
}
});

关于java - 一列添加两个按钮并获取点击行值javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com