JavaFX2.2 not all table columns showing data with PropertyValueFactory?

巧了我就是萌 提交于 2019-12-25 05:32:03

问题


Goodevening everyone,

I stumbled on a rather strange problem today and I have no clue what I'm doing wrong. It's about binding table columns with a PropertyValueFactory. I created the table view and table columns in FXML and set the appropriate fx:id's with Scene Builder.

Now the problem is that some columns are showing the expected data and other columns are just empty. I add the items to the table view with following code when a user presses a button:

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

I know for sure that the user objects in olUsers contain all the data needed (tested it with a for loop and System.out.println)

I get a result like this: (<text> representing the column headers)

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

So for some reason colums username, lastname, firstname, zip are not showing any data...

Some parts of the controller:

//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"));
...
}      

Here are some parts of the User class

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)

Sorry for the loads of code, but I really don't see why only a few columns are getting their data.. I hope someone can point me in the right direction :)

Thanks in advance!


回答1:


If your setter is named getFirstName then property is firstName with capital "N". Try next:

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"));

Also I would advise you to add property methods to User class:

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

it will allow your table to reflect data changes. See 2nd part of Binding JavaFX 2 TableView elements



来源:https://stackoverflow.com/questions/11976461/javafx2-2-not-all-table-columns-showing-data-with-propertyvaluefactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!