I am trying to create a ListView with a observableArrayList consisting of Commands. A Command class looks like this
public class Command {
private StringProperty commandName = new SimpleStringProperty(this, "commandName", "");
public Command(String name) {
setCommandName(name);
}
public void setCommandName(String name) {
this.commandName.set(name);
}
public String getCommandName() {
return commandName.get();
}
public StringProperty commandName() {
return commandName;
}
public String toString() {
return getCommandName();
}
}
I am using the commands in the following way:
Command cmd1 = new Command("Move Forward");
Command cmd2 = new Command("Turn Left");
Command cmd3 = new Command("Turn Right");
ObservableList<Command> lst = FXCollections.observableArrayList();
lst.add(cmd1);
lst.add(cmd2);
lst.add(cmd3);
ListView<Command> lv = new ListView<Command>();
lv.setItems(lst);
TextField tf = new TextField();
tf.textProperty().addListener((a,b,c) -> { cmd2.setCommandName(c); });
BorderPane bp = new BorderPane();
bp.setCenter(lv);
bp.setBottom(tf);
Scene scene = new Scene(bp,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
I would expect that updating cmd2
whenever the textfield changes would be reflected on the listview. But I am apprently misunderstanding something. Can anyone give a hint?