JavaFX2.2不是所有的表列都显示了PropertyValueFactory的数据?

时间:2012-08-15 20:07:40

标签: java javafx-2 tablecolumn

为每个人服务,

我今天偶然发现了一个相当奇怪的问题,我不知道自己做错了什么。它是关于使用PropertyValueFactory绑定表列的。我在FXML中创建了表视图和表格列,并使用Scene Builder设置了相应的fx:id' s。

现在的问题是某些列显示了预期的数据而其他列只是空的。当用户按下按钮时,我使用以下代码将项添加到表视图中:

ObservableList<User> olUsers = DA_User.getUsers();
tbl_MC_Manage_Users.getItems().addAll(olUsers);

我确信olUsers中的用户对象包含所需的所有数据(使用for循环和System.out.println测试)

我得到这样的结果:(&lt; text&gt;表示列标题)

<disabled>   <username>    <title>    <lastname>    <firstname>   <address>   <zip>    <city>
false        (empty)       Mr.        (empty)       (empty)       address1    (empty)  city1
false        (empty)       Miss       (empty)       (empty)       address2    (empty)  city2

因此,colums username,lastname,firstname,zip没有显示任何数据...

控制器的某些部分:

//declaration (these are the fx:id's connected to my table colums)
@FXML private TableView<User> tbl_MC_Manage_Users;
@FXML private TableColumn<User,Boolean> tc_MC_Manage_Disabled;
@FXML private TableColumn<User,String> tc_MC_Manage_Username;
@FXML private TableColumn<User,String> tc_MC_Manage_Title;
@FXML private TableColumn<User,String> tc_MC_Manage_Lastname;
@FXML private TableColumn<User,String> tc_MC_Manage_Firstname;
@FXML private TableColumn<User,String> tc_MC_Manage_Address;
@FXML private TableColumn<User,String> tc_MC_Manage_ZipCode;
@FXML private TableColumn<User,String> tc_MC_Manage_City;

@Override
public void initialize(URL url, ResourceBundle rb) {
...
//setting the binding on the table columns at initialize();
    tc_MC_Manage_Disabled.setCellValueFactory(new PropertyValueFactory<User, Boolean>("disabled"));
    tc_MC_Manage_Username.setCellValueFactory(new PropertyValueFactory<User, String>("username"));
    tc_MC_Manage_Title.setCellValueFactory(new PropertyValueFactory<User, String>("title")); 
    tc_MC_Manage_Firstname.setCellValueFactory(new PropertyValueFactory<User, String>("firstname"));
    tc_MC_Manage_Lastname.setCellValueFactory(new PropertyValueFactory<User, String>("lastname"));  
    tc_MC_Manage_Address.setCellValueFactory(new PropertyValueFactory<User, String>("address"));
    tc_MC_Manage_ZipCode.setCellValueFactory(new PropertyValueFactory<User, String>("zipcode")); 
    tc_MC_Manage_City.setCellValueFactory(new PropertyValueFactory<User, String>("city"));
...
}      

以下是User类的一些部分

private SimpleBooleanProperty disabled = new SimpleBooleanProperty();
private SimpleStringProperty username = new SimpleStringProperty("");
private SimpleStringProperty title = new SimpleStringProperty("");
private SimpleStringProperty lastname = new SimpleStringProperty("");
private SimpleStringProperty firstname = new SimpleStringProperty("");
//... (same for the others)

//getters
public boolean isDisabled() { return disabled.get(); }
public String getTitle() { return title.get(); }
public String getUsername() { return username.get(); }
public String getLastName() { return lastname.get(); }
public String getFirstName() { return firstname.get(); }
//... (same for the others)

//setters
public void setDisabled(boolean value) { disabled.set(value); }
public void setUsername(String value) { username.set(value); }
public void setTitle(String value) { title.set(value); }
public void setLastName(String value) { lastname.set(value); }
public void setFirstName(String value) { firstname.set(value); }  
//... (same for the others)

对于大量的代码感到抱歉,但我真的不明白为什么只有少数几列获取他们的数据..我希望有人可以指出我正确的方向:)

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果您的setter名为getFirstName,那么属性为firstName,大写为“N”。尝试下一步:

tc_MC_Manage_Disabled.setCellValueFactory(new PropertyValueFactory<User, Boolean>("disabled"));
tc_MC_Manage_Username.setCellValueFactory(new PropertyValueFactory<User, String>("userName"));
tc_MC_Manage_Title.setCellValueFactory(new PropertyValueFactory<User, String>("title")); 
tc_MC_Manage_Firstname.setCellValueFactory(new PropertyValueFactory<User, String>("firstName"));
tc_MC_Manage_Lastname.setCellValueFactory(new PropertyValueFactory<User, String>("lastName"));  
tc_MC_Manage_Address.setCellValueFactory(new PropertyValueFactory<User, String>("address"));
tc_MC_Manage_ZipCode.setCellValueFactory(new PropertyValueFactory<User, String>("zipCode")); 
tc_MC_Manage_City.setCellValueFactory(new PropertyValueFactory<User, String>("city"));

另外,我建议您将property方法添加到User类:

public StringProperty titleProperty() { return title; }; // etc

它将允许您的表反映数据更改。见Binding JavaFX 2 TableView elements

的第二部分