在一列中添加两个按钮并获取点击行值javafx
问题描述:
我想在javafx中的表视图的一列中实现两个按钮。我能够在一列中实现一个按钮,但无法在一列中添加两个按钮。 我有一些想法从这个链接在一列中添加两个按钮并获取点击行值javafx
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);
}
});
在,不出现的动作列按钮不会。我在哪里做错了。请任何人都让我知道。
答
由于缺乏cellValueFactory
项目为细胞tenthColumn
是总是null
。使用updateItem
方法的第二个参数来确定该行是否为空。
此外,为了说明这一点,我会将列的类型更改为TableColumn<Student, Void>
。
此外再次每次项改变是不必要的,因为在更新处理程序onAction
时间加入HBox
的儿童,由于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);
}
});