在一列中添加两个按钮,然后获取单击行值javafx [英] Add two button in one column and get the click row value javafx
问题描述
我正在尝试在javafx的表视图中的一列中实现两个按钮。我,能够在一列中实现一个按钮,但不能在一列中添加两个按钮。
我从这个链接中得到了一些想法
在,操作列按钮不会出现。我在哪里做错了。请有人告诉我。
缺少 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(编辑);
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 ,空);
setGraphic(空?null:窗格);
}
});
I am trying to implement two button in one column in table view in javafx . I, am able to implement one button in one column but not able to add two button in one column . I got some idea from this link
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
I have use the idea in the link above. However, the code is not working for me.
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);
}
});
In, the action column buttons doesn't appears. Where am I doing mistake. Please anyone let me know.
Lacking a cellValueFactory
the item for cells in tenthColumn
is always null
. Use the second argument of the updateItem
method to determine if the row is empty or not.
Also to indicate this I'd change the type of the column to TableColumn<Student, Void>
.
Furthermore adding the children of the HBox
again every time the item is changed is unnecessary as is updating the onAction
handlers, since getIndex()
is evaluated at the time the Button
is clicked.
You could change the code to this:
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);
}
});
这篇关于在一列中添加两个按钮,然后获取单击行值javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!