There is a FlowPane. Assume:

@FXML private FlowPane FlowPane; 

Objects of type ImageView (with pictures) were added to it. I want to delete all these objects with FlowPane. I'm trying to:

 FlowPane.getChildren().removeAll(); 

Or

 FlowPane.getChildren().clear(); 

With the first, nothing happens at all. The second is better. The objects disappear, but if I add a new object to FlowPane , all deleted objects appear again. clear(); method clear(); makes them seem invisible until the next update FlowPane . So actually how to do this? Maybe you need after the removal of something like update(); or reload(); ?

 public class MainController implements Initializable { private static ArrayList<Item> ItemsList = new ArrayList<>(); @FXML private ScrollPane myScrollPane; @FXML private ImageView TestImageView; private static class Item { public String ItemsName; public String ItemsImage; public int ItemsId; public int ItemsPrice; public String getItemsName() { return ItemsName; } public String getItemsImage() { return ItemsImage; } public int getItemsId() { return ItemsId; } public int getItemsPrice() { return ItemsPrice; } } @FXML private FlowPane FlowPane; @FXML private Button testbutton; private File file; private int events; @Override public void initialize(URL url, ResourceBundle rb) { FlowPane.setMaxWidth(500); FlowPane.setPadding(new Insets(5, 5, 5, 5)); FlowPane.setVgap(10); FlowPane.setHgap(5); } @FXML private void clicktestbutton(ActionEvent event) throws XMLStreamException, FileNotFoundException { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader; reader = factory.createXMLStreamReader(new FileInputStream("C:\\Users\\Пользователь\\Documents\\NetBeansProjects\\PriceCalculationForMinecraft\\src\\me\\pricecalculationforminecraft\\items\\Basic_items.xml")); events = reader.getEventType(); while (true) { switch (events) { case XMLStreamConstants.START_ELEMENT: switch (reader.getLocalName()) { case "Item": Item _item = new Item(); _item.ItemsName = reader.getAttributeValue(0); _item.ItemsImage = reader.getAttributeValue(1); _item.ItemsId = Integer.parseInt(reader.getAttributeValue(2)); _item.ItemsPrice = Integer.parseInt(reader.getAttributeValue(3)); ItemsList.add(_item); break; } break; } if (!reader.hasNext()) { break; } events = reader.next(); } for (Item _item : ItemsList) { ImageView image = new ImageView(new Image(getClass().getResourceAsStream(_item.getItemsImage()))); image.setId(Integer.toString(_item.getItemsId())); image.setFitHeight(40); image.setFitWidth(40); FlowPane.getChildren().add(image); //FlowPane. image.setOnMouseClicked(e -> { try { System.out.println(getIdImage(image)); } catch (SecurityException | NoSuchFieldException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); } }); System.out.printf("%s | %s | %s | %s\n", _item.getItemsName(), _item.getItemsImage(), _item.getItemsId(), _item.getItemsPrice()); } } private String getIdImage(ImageView image) throws SecurityException, NoSuchFieldException { TestImageView.setImage(image.getImage()); //System.out.println(image.getImage().getClass().getField("ItemsPrice")); //FlowPane.getChildren().removeAll(image); FlowPane.getChildren().clear(); return image.getId(); } 

}

Here is a video with an error that would be clearer. https://www.youtube.com/watch?v=8pMZznzLnpI&feature=youtu.be

  • one
    removeAll() should not have worked, since according to the signature, it is necessary to transfer the array to be deleted. I could not reproduce the problem with clear() - everything worked out clearly. Maybe the problem is not with the removal, but with the addition of a new one? (need more code) - Andrey M
  • Added class code in the first post. - Prototype - TV
  • Here is a video with an error that would be clearer. - Prototype - TV

1 answer 1

As it was supposed, the jamb in the addendum: you FlowPane everything out of FlowPane , but fill it in from the original ItemsList containing the old data.

Solution option:

 FlowPane.getChildren().clear(); ItemsList.clear(); 
  • Thank you very much, everything works. - Prototype - TV