0

我是这里的新手,我在 JavFX 中的表视图即 tblStudent 不包含
我想要的记录。如何做到这一点,请帮助我。

    public void populateData(){
      Connection c ;
      data = FXCollections.observableArrayList();
      try{
        c = ConnectionClass.connect();
        //SQL FOR SELECTING ALL OF CUSTOMER
        String SQL = "SELECT * FROM `student`";
        //ResultSet
        ResultSet rs = c.createStatement().executeQuery(SQL);


        studentIDCol = new TableColumn("Student ID");
        nameCol = new TableColumn("Name");
        addressCol = new TableColumn("Address");
        ageCol = new TableColumn("Age");
        contactnoCol = new TableColumn("Contact Number");

        while(rs.next()){
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();

            row.add(rs.getString("id"));
            row.add(rs.getString("name"));
            row.add(rs.getString("address"));
            row.add(rs.getString("age"));
            row.add(rs.getString("contact_num"));

            System.out.println("Row [1] added "+row );
            data.add(row);

        }

        //FINALLY ADDED TO TableView
        tblStudent.getItems().setAll(data);
      }catch(Exception e){
          e.printStackTrace();
          System.out.println("Error on Building Data");             
      }
 }

有谁知道这个?

4

1 回答 1

3

您需要将单元格值工厂设置为所有列,如下所示:

TableColumn<ObservableList<String>, String> studentIDCol  = new TableColumn<>("Student ID");
    studentIDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {         
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(0));
        }
    });

而且我还建议您将查询提取到服务中,因为该查询会阻塞 JavaFX 线程。查看http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

用测试数据检查这个完整的例子。

public void start(Stage stage) throws Exception {
    VBox vbox = new VBox(10.0);
    vbox.setAlignment(Pos.CENTER);

    final TableView<ObservableList<String>> table = new TableView<>();

    TableColumn<ObservableList<String>, String> studentIDCol  = new TableColumn<>("Student ID");
    studentIDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {         
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(0));
        }
    }); 

    TableColumn<ObservableList<String>, String> nameCol = new TableColumn<>("Name");
    nameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {          
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(1));
        }
    });     

    TableColumn<ObservableList<String>, String> addressCol  = new TableColumn<>("Address");
    addressCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {           
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(2));
        }
    });     

    TableColumn<ObservableList<String>, String> ageCol  = new TableColumn<>("Age");
    ageCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {           
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(3));
        }
    });     

    TableColumn<ObservableList<String>, String> contactnoCol  = new TableColumn<>("Contact Number");
    contactnoCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {         
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(4));
        }
    });     

    table.getColumns().add(studentIDCol);
    table.getColumns().add(nameCol);
    table.getColumns().add(addressCol);
    table.getColumns().add(ageCol);
    table.getColumns().add(contactnoCol);   

    final ProgressBar bar = new ProgressBar(-1);
    bar.setVisible(false);

    final Button button = new Button("Fill");
    button.setOnAction(new EventHandler<ActionEvent>() {            
        @Override
        public void handle(ActionEvent event) {
            final Service<ObservableList<ObservableList<String>>> service = new Service<ObservableList<ObservableList<String>>>() {                 
                @Override
                protected Task<ObservableList<ObservableList<String>>> createTask() {
                    return new Task<ObservableList<ObservableList<String>>>() {
                        @Override
                        protected ObservableList<ObservableList<String>> call() throws Exception {
                            ObservableList<ObservableList<String>> items = FXCollections.observableArrayList();
                            items.add(FXCollections.observableArrayList("1", "2", "3", "4", "5"));
                            items.add(FXCollections.observableArrayList("1", "2", "3", "4", "5"));
                            items.add(FXCollections.observableArrayList("1", "2", "3", "4", "5"));
                            //Here the code for the query
                            Thread.sleep(5000);//To see the progress
                            return items;
                        }
                    };
                }
            };

            bar.visibleProperty().bind(service.runningProperty());
            button.disableProperty().bind(service.runningProperty());
            table.itemsProperty().bind(service.valueProperty());

            service.start();
        }
    });     

    vbox.getChildren().addAll(table, button, bar);
    Scene scene = new Scene(vbox, 300, 300);
    stage.setScene(scene);
    stage.show();
}

如果您使用的是 FXML 文件。您需要跳过 TableColumn 的初始化步骤。

使用 @FXML 注释将 TableColumn 声明为控制器类中文件中的字段。确保实现 javafx.fxml.Initializable。在初始化方法中,您可以为每一列进行配置。并避免将列添加到表中,因为它们已经在表视图中。

如果您像这样在 FXML 中定义 TableView,这是一个示例:

<TableView layoutX="145.0" layoutY="76.0" prefHeight="200.0" prefWidth="200.0">
  <columns>
    <TableColumn prefWidth="75.0" text="Student ID" fx:id="studentIDCol" />
    <TableColumn prefWidth="75.0" text="Name" fx:id="nameCol" />
    <TableColumn prefWidth="75.0" text="Address" fx:id="addressCol" />
    <TableColumn prefWidth="75.0" text="Age" fx:id="ageCol" />
  </columns>
</TableView>

然后你的控制器类将是这样的:

class YourTest implements javafx.fxml.Initializable{
//More Code
@FXML
TableColumn<ObservableList<String>, String> studentIDCol;
@FXML
TableColumn<ObservableList<String>, String> nameCol;
@FXML
TableColumn<ObservableList<String>, String> addressCol;
@FXML
TableColumn<ObservableList<String>, String> ageCol;
//More of your code 
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    studentIDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {         
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(0));
        }
    }); 

    nameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {          
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(1));
        }
    });             

    addressCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {           
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(2));
        }
    });     

    ageCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {           
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(3));
        }
    });     

    contactnoCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>() {         
        @Override
        public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) {     
            return new SimpleStringProperty(cdf.getValue().get(4));
        }
    });  
}
//More of your Code
}

希望能帮助到你。

于 2013-06-04T10:14:16.057 回答