Issue
I am making a password manager using JavaFX and I am holding all the relevant info from the user's account in a listview and they can select the item in the list to display it on a form to the right. At this point the listview is just displaying text relevant to the object that is is holding. I would like to display the name of the site that the account info is for. I have a getSiteName() method on the AccountInfo class, but I don't know how to set the text in the list view. Looking for some guidance here! Thanks.
Solution
You should set a csutom CellFactory and override how it renders item's text by overrideing updateItem
method:
lv.setCellFactory(new Callback<ListView<AccountInfo>, ListCell<AccountInfo>>() {
@Override
public ListCell<AccountInfo> call(ListView<AccountInfo> param) {
ListCell<AccountInfo> cell = new ListCell<AccountInfo>() {
@Override
protected void updateItem(AccountInfo item, boolean empty) {
super.updateItem(item, empty);
if(item != null) {
setText(item.getSiteName());
} else {
setText(null);
}
}
};
return cell;
}
});
Answered By - Omid
Answer Checked By - Senaida (JavaFixing Volunteer)