I'm setting up a ListView from an Observable list which has an input of another collection (in this case, a linked list). So i found in this answer how to make the items of the list view get removed from it (I'm not pretty sure if they are removed from the ObservableList as well), so theres any possible way to make the modifications in both collections (i.e. the ObservableList and the original collection)?
Here's a piece of the code:
LinkedList<> shoppingCart; //In the code this has been initialized before.
public static class XCell extends ListCell<Product> {
HBox hb = new HBox();
Label name = new Label("");
Pane p = new Pane();
Button d = new Button("X");
public XCell() {
super();
File f = new File("src/style/main.css");
hb.getStylesheets().clear();
hb.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\\", "/"));
hb.getChildren().addAll(nombre, p, d);
HBox.setHgrow(p, Priority.ALWAYS);
d.getStyleClass().add("red-btn");
d.setOnAction(event -> getListView().getItems().remove(getItem()));
}
@Override
protected void updateItem(Product item, boolean empty) {
super.updateItem(item,empty);
setText(null);
setGraphic(null);
if (item != null && !empty) {
nombre.setText(item.toString());
setGraphic(hb);
}
}
}
private void showCart(ActionEvent event){
ObservableList<Product> cart = FXCollections.observableArrayList(shoppingCart);
ListView<Alimento> lv = new ListView<>(cart);
lv.setCellFactory(param -> new XCell());
Scene sc = new Scene(lv);
Stage st = new Stage();
st.setScene(sc);
st.setTitle("Pizza! -- Cart");
st.show();
}