ListView_Bind_getItems1
  • lvwList2.getItems().add(newValue);
    lvwList1.itemsProperty().bind(listProperty);
    label1.textProperty().bind(strBind1);
Code:

package javafxtemplate1;
// List to ListView1
//ListView_Bind_getItems1

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
*
* @author Manas9
*/
public class JavaFXTemplate1 extends Application {

Foo2 foo2 = new Foo2();
final TextArea txtArea = new TextArea();
Label label1 = new Label("Enter Texts");
ListProperty<String> listProperty = new SimpleListProperty<>();
@Override
public void start(Stage primaryStage) throws Exception {
//
List<String> list1 = new ArrayList<>();
ListView<String> lvwList2 = new ListView<String>();
ObservableList<String> set1 = FXCollections.observableList(list1);
ListView<String> lvwList1 = new ListView<>((ObservableList<String>) set1);
//lvwList1 settings
lvwList1.setLayoutX(10); lvwList1.setLayoutY(0);
lvwList1.setMaxWidth(120);lvwList1.setMaxHeight(180);
//
lvwList2.setLayoutX(150); lvwList2.setLayoutY(0);
lvwList2.setMaxWidth(120);lvwList2.setMaxHeight(180);

Group groot1 = new Group();
Group groot2 = new Group();
//
label1.setLayoutX(10);label1.setLayoutY(20);
TextField textField = new TextField ();
textField .setLayoutX(160);textField .setLayoutY(20);
textField.setPromptText("Text Field");
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
//throw new UnsupportedOperationException("Not supported yet.");
foo2.setFoo2("@");
newValue += foo2.getFoo2();
foo2.setFoo2("$");
newValue += foo2.getFoo2();
// label1.setText( newValue);

StringBinding strBind1= Bindings.selectString(textField.textProperty());
listProperty.set(FXCollections.observableArrayList(newValue));
lvwList2.getItems().add(newValue);
lvwList1.itemsProperty().bind(listProperty);
label1.textProperty().bind(strBind1);

}
});

//textField.setOnKeyTyped(e -> handlekeyevent(e));
BorderPane bpane = new BorderPane();
groot1.getChildren().addAll(textField,label1);
groot2.getChildren().addAll( lvwList2, lvwList1);
//groot2.getChildren().addAll( txtArea);
bpane.setCenter(groot2);
bpane.setTop(groot1);
//Scene scene = new Scene(groot, 350, 150);
Scene scene = new Scene(bpane, 350, 250);
primaryStage.setScene(scene);
primaryStage.setTitle("ObservableSet:: ChangeListener");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

}
 

Class Foo2
package javafxtemplate1;

public class Foo2 {
public String strFoo2;
public String getFoo2(){
return strFoo2;
}
public void setFoo2(String param1)
{
strFoo2 = param1;
}
}
 

 

Runtime displays:

Binding with listProperty is dynamic, meaning input in the textfield will update the string in ListView(listProperty). Similarly StringBinding  in Binding class, can bridge two controls dynamically