TextField_TextAreaListview2
  • With Lambda Expresion
  • textField.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
    txtArea.appendText("Text Changed :" + newValue + ")\n");
    set1.add(newValue);
    });
Code:

package javafxtemplate1;

//textProperty().addListener Listview Label
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
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.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;


/**
*
* @author Manas9
*/
public class JavaFXTemplate1 extends Application {
List<String> list1 = new ArrayList<>();
ObservableList<String> set1 = FXCollections.observableList(list1);
final TextArea txtArea = new TextArea();
Label label1 = new Label("Enter your name");
String str1="";
@Override
public void start(Stage primaryStage) throws Exception {
//
ListView<String> lvwList1 = new ListView<>((ObservableList<String>) set1);
lvwList1.setLayoutX(160); lvwList1.setLayoutY(0);
lvwList1.setMaxWidth(120);lvwList1.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("Enter Your Name");
textField.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
txtArea.appendText("Text Changed :" + newValue + ")\n");
set1.add(newValue);
 label1.setText(newValue);
});

BorderPane bpane = new BorderPane();
groot1.getChildren().addAll(textField,label1);
groot2.getChildren().addAll(txtArea, lvwList1);
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);
}

}

 

Runtime displays: