1

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?

Peter
  • 249
  • 1
  • 3
  • 9
  • possible duplicate of [JavaFX: Update of ListView if an element of ObservableList changes](http://stackoverflow.com/questions/13906139/javafx-update-of-listview-if-an-element-of-observablelist-changes) – eckig May 18 '15 at 10:28
  • Rename the "public StringProperty commandName()" to "public StringProperty commandNameProperty()" in Command class and try again. – Uluk Biy May 18 '15 at 10:44

0 Answers0