使用javafx填充TableView

时间:2016-11-19 07:03:45

标签: javafx tableview

我的resultSet似乎工作正常,但我无法将数据设置为我的TableView。 ResultSet正确地为我提供了一些数据行,但是当我将行数据传递给我的表时,它不起作用。 (让我疯狂!!)

public class AllStudentsTable extends TableView {

TableColumn identitycol = new TableColumn("Personal Identity");
TableColumn name = new TableColumn("First Name");
TableColumn lastname = new TableColumn("Last Name");
TableColumn fathername = new TableColumn("Father Name");
TableColumn age = new TableColumn("Age");
TableColumn idcardno = new TableColumn("Identity Card No");
TableColumn registerExcatDate = new TableColumn("Register Date");

TableColumn schoolidentitycol = new TableColumn("School Identity");
TableColumn schoolIdcol = new TableColumn("School Id");
TableColumn classcol = new TableColumn("Class");
TableColumn phasecol = new TableColumn("Phase");

Statement stmt;

ObservableList<ObservableList> data;
ObservableList<String> row;

public AllStudentsTable() {

    identitycol.getColumns().addAll(name, lastname, fathername, age, idcardno, registerExcatDate);
    schoolidentitycol.getColumns().addAll(schoolIdcol, classcol, phasecol);
    this.getColumns().addAll(identitycol, schoolidentitycol);
    //this.setItems(data);
    buildData();

}

//CONNECTION DATABASE
private void buildData() {
    Connection c;
    data = FXCollections.observableArrayList();
    try {
        c = DriverManager.getConnection("jdbc:mysql://localhost/School Manager", "root", "");
        //SQL FOR SELECTING ALL OF CUSTOMER
        String SQL = "SELECT name,lastname,fathername,age,identitycardnumber,exactDate,schoolId, class, phase from students";
        //ResultSet
        ResultSet rs = c.createStatement().executeQuery(SQL);

        /**
         * ********************************
         * TABLE COLUMN ADDED DYNAMICALLY * ********************************
         */

        /**
         * ******************************
         * Data added to ObservableList * ******************************
         */
        while (rs.next()) {
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                //Iterate Column
                row.add(rs.getString(i));
            }
            System.out.println("Row [1] added " + row);
            data.add(row);
        }

        //FINALLY ADDED TO TableView

        this.setItems(data);
    } catch (Exception e) {
        System.out.println("Error on Building Data");
    }
}

}

0 个答案:

没有答案