1

i have the following method:

CreateStrip createStrip = new CreateStrip(input);
depBox.getChildren().add(createStrip.getStripGrid());

depBox is a VBox and .getStripGrid() return a GridPane. CreateStrip has this method too:

public String getNameStrip() { return input.getNameStrip();}

during the life of the program depBox get many GridPane, each one with a different NameStrip. sometimes i have the necessity to remove a specific GridPane from depBox that match with .getNameStrip(). i have tried:

    for (Node node: depBox.getChildren()) {

//TODO REMOVE GRIDPANE CONTAIN THE NAME THAT MATCH WITH THE MESSAGE RECEIVED..
            }

but i don't know how to set the matching control.

Daniele_r81
  • 123
  • 3
  • 14

1 Answers1

1

Step 1:

Attach data to the GridPane that allows you to identify the one to remove.

You could do this by using Node.setUserData or by using the properties Map (which I'll do in the following code snippets).

Creating the GridPane

GridPane gp = createStrip.getStripGrid();
gp.getProperties().put(NAME_KEY, createStrip);
depBox.getChildren().add(gp);
// use object not equal to any other object as key (i.e. equals uses reference equality)
static final Object NAME_KEY = new Object(); 

Step 2:

Use the information to remove the appropriate GridPane. searchName is the String that identifies the GridPane you want to remove (to check for equality with getNameStrip()):

depBox.getChildren().removeIf(c -> {
     CreateStrip strip = (CreateStrip) c.getProperties().get(NAME_KEY);
     return strip != null && searchName.equals(strip.getNameStrip());
});

Depending on your CreateStrip class it may not be necessary to add a instance of it as property. It may not even be the right thing to do, if it's a factory, but I think you get the idea nontheless.

Alternative

You can also assign a value to the id property of the Node and use those to identify the correct node using Node.lookup. However those need to be unique and be a valid css id, but you could use a Map to map from message to id.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • how can i define my NAME_KEY as static final Object NAME_KEY = new Object(); ? i receive a String so the object in my case should be static final Object "String" = new Object()...but i can't do that. – Daniele_r81 Mar 07 '16 at 06:19
  • one more thing, searchName.equals what does it mean? – Daniele_r81 Mar 07 '16 at 06:30
  • @Daniele_r81: `NAME_KEY` is the key for the `properties` map of the `GridPane`s. `searchName` is just a placeholder for the `String` to search. What `equals` means? See here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – fabian Mar 07 '16 at 09:17
  • really sorry but i didn't get it before...ill try as soon as possible and ill give you my feedback. thanks. – Daniele_r81 Mar 07 '16 at 09:23
  • yes it work...thanks a lot. May i use this method even for update some filed inside the gridPane? – Daniele_r81 Mar 07 '16 at 12:14
  • ok done...i use the same concept fabian suggest me. thanks again – Daniele_r81 Mar 07 '16 at 12:26
  • @Daniele_r81 Of course you can use `depBox.getChildren().stream().filter(predicate).findFirst()` with the same `Predicate` used with `removeIf` in my answer. We're just attaching information to the `Node` that can be used to identify it here... – fabian Mar 07 '16 at 12:27