I'm implementing a desktop chat application using JavaFX. I'm using a listview to show contacts.
I customized the list cell by following this link JavaFX custom cell factory with custom Objects.
When a contact becomes online/offline, the server notifies me appropriately, so I need to change the color of online icon accordingly.
Below is my code...
File: MainController.java
public class MainController implements Initializable {
@FXML
private ListView<ContactInfo> contactListView;
private ObservableList<ContactInfo> contactList = FXCollections.observableArrayList();
this.contactListView.setCellFactory( listView -> {
return new ContactCell();
}
}
File: ContactCell.java
public class ContactCell extends ListCell<ContactInfo> {
private final ContactItemController controller = new ContactItemController();
public ContactCell() {
}
@Override
protected void updateItem(ContactInfo item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
controller.setItem(item);
setGraphic(controller.getView());
}
}
}
File: ContactItemController.java
public class ContactItemController {
@FXML
private Pane pane;
@FXML
private ImageView contactImage;
@FXML
private Label contactName;
@FXML
private FontAwesomeIconView onlineIcon;
public ContactItemController() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/snippets/ContactItem.fxml"));
fxmlLoader.setController(this);
try {
pane = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setItem(ContactInfo item) {
this.contactName.setText(item.getName());
if(item.getOnline().getOnline())
this.onlineIcon.setFill(Color.LIGHTGREEN);
else
this.onlineIcon.setFill(Color.web("#838383"));
}
public Pane getView() {
return this.pane;
}
}
File: ContactInfo.java
public class ContactInfo {
private String name;
private BooleanProperty online;
// Getters and setters
..............
..............
}
I tried to add change listener to each item's boolean property inside setItem() method of the ContactItemController class.
But the listener is getting added more than once.... Is this the right way to do this?