button - 在同一行JavaFX中单击按钮从tableview中获取行
问题描述
我在 JavaFX 中创建了一个 TableView,它包含两列:
- 第一行(卷):它包含卷号。学生的
- 第二行(详细信息):它包含一个按钮,单击该按钮会打印相应行的“滚动”。
我的方法是:
static ObservableList<Student> list= FXCollections.observableArrayList();
static ObservableList<Student> getStudentList() throws SQLException {
con = ConnectionUtil.getDataBaseConnection();
String SQL = "Select * from student;";
ResultSet rs = con.createStatement().executeQuery(SQL);
while(rs.next()){
//Student cm = new Student(rs.getInt("rno"), rs.getString("name"),false);
Student cm = new Student();
cm.setFullName(rs.getString("name"));
cm.setRoll(rs.getString("rno"));
cm.setSingle(false);
list.add(cm);
}
return list;
}
static TableColumn<Student, String> rollCol = new TableColumn<Student, String>("Roll");
static TableColumn<Student, Void> viewCol = new TableColumn<Student, Void>("Details");
static TableColumn<Student, Void> addButtonToTable() {
//TableColumn<Student, Void> viewCol = new TableColumn("Button Column");
Callback<TableColumn<Student, Void>, TableCell<Student, Void>> cellFactory = new Callback<TableColumn<Student, Void>, TableCell<Student, Void>>() {
@Override
public TableCell<Student, Void> call(final TableColumn<Student, Void> param) {
final TableCell<Student, Void> cell = new TableCell<Student, Void>() {
private final Button btn = new Button("View");
{
btn.setOnAction((ActionEvent event) -> {
/////////////////WORK START FROM HERE
static Student stu=tables.getItems();
String res = (stu.getRollNo().toString());
System.out.println(res);
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(btn);
}
}
};
return cell;
}
};
viewCol.setCellFactory(cellFactory);
return viewCol;
}
public String getRollNo() {
return rollNo;
}
public void setRoll(String rollNo) {
this.rollNo = rollNo;
}
我的问题:
- 我的表很好地显示了两列
- 但它不会在输出中打印“滚动”。
请告诉我我该怎么做...在此先感谢
解决方案
static
不允许声明局部变量并TableView.getItems
返回 a ObservableList
,而不是Student
。通过访问Student
实例TableRow
。(如果您将所有按钮显示在一行中,而不是在一列中,则所需的逻辑将非常不同。)
private final Button btn = new Button("View");
{
btn.setOnAction((ActionEvent event) -> {
Student stu = getTableRow().getItem();
String res = stu.getRollNo().toString();
System.out.println(res);
});
}
@Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : btn);
}
推荐阅读
- r - 如何使用具有许多缺失值的回归模型进行预测?
- google-data-studio - 将邮政编码从 BigQuery 映射到 DataStudio
- r - 定位和计算同一行上的重复元素 - R
- azure - 如何使用 Azure API 管理为 API 中的每个方法设置 405 Method Not Allowed
- heroku - 无法将回购推送到 Heroku
- react-native - React-Native-Elements:在圆形头像的中心显示图像组件
- html - 使用 componentsSeperatedByString 不工作目标 c 基于 span id 拆分网页源字符串
- qt - 如何在 C++ 中使用包含另一个共享库的共享库
- javascript - Intl.DateTimeFormat 在节点和浏览器中以不同的方式工作
- python - Feedparser,没有 etag 和修改的属性