我是这里的新手,我在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");
}
}
有谁知道这个?
答案 0 :(得分: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。在initialize方法中,您可以为每个列执行配置端。并避免将列添加到表中,因为它们已经在表视图中。
如果您要在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
}
希望它有所帮助。