在javafx表视图中,不显示

时间:2017-09-30 23:15:25

标签: javafx tableview javafx-8

Refer Image of Output
//数据库已成功连接 我正在尝试创建一个表格,我想在其中显示我的学生的内容。在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;           
    }

}

2 个答案:

答案 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