我已经在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;
}
我的问题:
请告诉我该怎么做... 预先感谢
答案 0 :(得分:0)
static
,并且TableView.getItems
返回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);
}