我知道这已被问过100次,但我还是无法让它发挥作用。 我希望TableView填充存储在我的数据库中的东西。
我用一个空的TableView得到了我的FXML(这里的第一个问题:我应该设置列吗?)
public void initialize() {
TableView tableview = getUserTable();
ObservableList<ObservableList> data;
Connection c;
data = FXCollections.observableArrayList();
try{
c = Database.getConnection();
String SQL = "SELECT * FROM User";
ResultSet rs = c.createStatement().executeQuery(SQL);
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column ["+i+"] ");
}
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
tableview.setItems(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
这是我的方法,可以将数据库中的内容输出到TableView中。我得到了som System out print来检查它是否给了我正确的东西而且确实如此。问题是我得到了NullPointerExpections&#34; tableview.getColums()。addAll.col&#34;
这是代码:
private Parent sceneUserEdit;
private UserEditView viewUserEdit;
public Parent userEditScene() throws SQLException {
Stage userEditStage = new Stage();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("../view/UserEdit.fxml"));
try {
sceneUserEdit = loader.load();
userEditStage.setTitle("SoPra JavaFX");
userEditStage.setScene(new Scene(sceneUserEdit, 600, 400));
userEditStage.show();
} catch (IOException e) {
e.printStackTrace();
}
viewUserEdit = loader.getController();
viewUserEdit.setSimpleController(this);
initialize();
return sceneUserEdit;
}
我通过该教程来获取方法:http://blog.ngopal.com.np/2011/10/19/dyanmic-tableview-data-from-database/comment-page-1/
编辑:
在控制器中,我有以下方法来显示场景。
@FXML
private TableView<User> userTable;
public TableView<User> getUserTable(){return userTable;}
初始化方法在Controller中。 在UserEditView类中,我得到了整个TableView的FXML,以及每个TableColumn和那些
的getter。@JsonIdentityInfo(generator=IntSequenceGenerator.class)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class Parent {
private List<Child> children=new ArrayList<>();
private String name="parent";
public Parent() {
super();
children.add(new Child(this,"foo"));
children.add(new Child(this,"bar"));
}
public List<Child> getChildren() {
return children;
}
}
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class Child {
private Parent parent;
private String pseudo="toto";
public Child(Parent parent, String pseudo) {
super();
this.parent = parent;
this.pseudo = pseudo;
}
}
public class SOJson {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
Parent object=new Parent();
String json1 = objectMapper.writeValueAsString( object);
System.out.println("write parent: "+json1);
String json2 = objectMapper.writeValueAsString(object.getChildren().get(0));
System.out.println("write child: "+json2);
}
}
答案 0 :(得分:1)
在您的FXML文件中
<TableView fx:id="userEditTable" ... />
但你在控制器中
@FXML
private TableView<User> userTable;
字段名称需要与fx:id
匹配。