//数据库已成功连接
我正在尝试创建一个表格,我想在其中显示我的学生的内容。在Javafx的tableView中的表,但我无法获得所需的输出。
ObservableList<Student> list = FXCollections.observableArrayList();
@FXML
//Initializes the controller class.
@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
initCol();
loadTable();
}
//A view Table has been made with fx:id-table
//Variable name for 2 columns are 'fx:id-rollnoColand' & 'fx:id-nameCol'
@FXML
private TableView<Student> table;
@FXML
private TableColumn<Student,String> rollnoCol;
@FXML
private TableColumn<Student,String> nameCol;
private void initCol()
{
rollnoCol.setCellValueFactory(new PropertyValueFactory<>("s_rollno"));
nameCol.setCellValueFactory(new PropertyValueFactory<>("s_name"));
}
//Name of the Table is 'student' with Columns 'rollno' and 'name'
private void loadTable()
{
String selectAll = "select * from student";
try
{
Statement stmt = connectdb.createStatement();
ResultSet rs = stmt.executeQuery(selectAll);
while(rs.next())
{
String getrollno = rs.getString("rollno");
String getname = rs.getString("name");
list.add(new Student(getrollno,getname));
}
}
catch(SQLException exp)
{
System.out.println(exp);
}
table.getItems().setAll(list);
}
public static class Student
{
private final String s_rollno;
private final String s_name;
Student(String rollno,String name)
{
this.s_rollno = rollno;
this.s_name = name;
}
}
答案 0 :(得分:1)
PropertyValueFactory
适用于getter或属性方法。在您的情况下,您需要在Student
类中为属性添加getter,以使PropertyValueFactory
能够检索值:
public static class Student {
private final String s_rollno;
private final String s_name;
Student(String rollno, String name) {
this.s_rollno = rollno;
this.s_name = name;
}
public String getS_rollno() {
return s_rollno;
}
public String getS_name() {
return s_name;
}
}
答案 1 :(得分:-2)
之前我遇到过这个问题,原因很多。首先,您可以从模型中删除final
关键字,因为您的变量正在发生变化。其次,TableView显示空单元格,因为您创建了变量{{1在类private
中,您无法从父类中的Student
访问它们。因此,您需要添加getter和setter方法来访问这些变量。
init method