Issue
I have a program with these columns:
showNameColumn.setCellValueFactory(new PropertyValueFactory<Show, String>("ShowName"));
epsWatchedColumn.setCellValueFactory(new PropertyValueFactory<Show, Integer>("EpsWatched;"));
showStatusColumn.setCellValueFactory(new PropertyValueFactory<Show, String>("ShowStatus"));
The getters are called getShowName(), getEpsWatched(), and getShowStatus() which get the variables showName, epsWatched, and showStatus.
What's wrong here? The strings display but not the ints.
Solution
You have a “;” in the string passed to the property value factory, it should not be there.
This is wrong:
new PropertyValueFactory<Show, Integer>("EpsWatched;")
PropertyValueFactory lookup strings and property names should follow Java bean naming conventions. For more info, see:
Normally, I’d close the question as a duplicate of the above question, however, the asker requested a specific answer and had some additional questions, so this answer addresses those directly.
It is recommended to use a lambda rather than a PropertyValueFactory, as any errors will be detected and reported accurately by the compiler rather than failures at runtime. For a thorough analysis, see:
how would I write a lamba for this?
In your model class declare the property and provide an accessor for it:
public class Show {
private final IntegerProperty numEpisodes = new SimpleIntegerProperty();
public final IntegerProperty numEpisodesProperty() {
return numEpisodes;
}
}
Use the property accessor in a lambda to define the cell value factory:
TableColumn<Show, Integer> numEpisodesCol = new TableColumn<>();
numEpisodesCol.setCellValueFactory(
data -> data.getValue().numEpisodesProperty().asObject()
);
See also:
Answered By - jewelsea
Answer Checked By - Marilyn (JavaFixing Volunteer)