11package com .baeldung .javafx .view ;
22
33
4+ import com .baeldung .javafx .model .Person ;
45import javafx .collections .FXCollections ;
56import javafx .collections .ObservableList ;
67import javafx .concurrent .Task ;
78import javafx .fxml .FXML ;
89import javafx .scene .Node ;
9- import javafx .scene .control .Button ;
10- import javafx .scene .control .Pagination ;
11- import javafx .scene .control .TextField ;
12- import javafx .scene .image .Image ;
10+ import javafx .scene .control .*;
1311import javafx .scene .input .KeyCode ;
1412import javafx .scene .layout .VBox ;
1513
16- import java .util .ArrayList ;
1714import java .util .List ;
15+ import java .util .stream .Collectors ;
1816
1917public class SearchController {
2018
@@ -26,41 +24,50 @@ public class SearchController {
2624 private Button searchButton ;
2725 @ FXML
2826 private Pagination pagination ;
27+ @ FXML
28+ private Label searchLabel ;
2929
30- private ObservableList <Image > masterData = FXCollections .observableArrayList ();
31-
30+ private ObservableList <Person > masterData = FXCollections .observableArrayList ();
3231
3332 public SearchController () {
34-
33+ masterData .add (new Person (5 , "John" , true ));
34+ masterData .add (new Person (7 , "Albert" , true ));
35+ masterData .add (new Person (11 , "Monica" , false ));
3536 }
3637
37- /**
38- * Initializes the controller class. This method is automatically called
39- * after the fxml file has been loaded.
40- * <p>
41- * Initializes the table columns and sets up sorting and filtering.
42- */
4338 @ FXML
4439 private void initialize () {
4540
4641 // search panel
4742 searchButton .setText ("Search" );
4843 searchButton .setOnAction (event -> loadData ());
4944
45+ searchButton .setStyle ("-fx-background-color: slateblue; -fx-text-fill: white;" );
46+
5047 searchField .setOnKeyPressed (event -> {
5148 if (event .getCode ().equals (KeyCode .ENTER )) {
5249 loadData ();
5350 }
5451 });
5552
53+ searchField .textProperty ().addListener ((observable , oldValue , newValue ) -> {
54+ searchLabel .setText (newValue );
55+ });
56+
5657 pagination .setPageFactory (SearchController .this ::createPage );
5758 }
5859
5960 private Node createPage (Integer pageIndex ) {
6061
6162 VBox iconContainer = new VBox ();
6263
63- //iconContainer.getChildren().add(myGrid);
64+ TableView <Person > tableView = new TableView <>(masterData );
65+ TableColumn id = new TableColumn ("ID" );
66+ TableColumn name = new TableColumn ("NAME" );
67+ TableColumn employed = new TableColumn ("EMPLOYED" );
68+
69+ tableView .getColumns ().addAll (id , name , employed );
70+ iconContainer .getChildren ().add (tableView );
6471
6572 return iconContainer ;
6673 }
@@ -69,29 +76,26 @@ private void loadData() {
6976
7077 String searchText = searchField .getText ();
7178
72- Task <List <String >> task = new Task <List <String >>() {
79+ Task <List <Person >> task = new Task <List <Person >>() {
7380 @ Override
74- protected List <String > call () throws Exception {
75- updateMessage ("Loading images " );
76-
77- List < String > result = new ArrayList <>();
78-
79- return result ;
81+ protected List <Person > call () throws Exception {
82+ updateMessage ("Loading data " );
83+ return masterData
84+ . stream ()
85+ . filter ( value -> value . getName (). toLowerCase (). contains ( searchText ))
86+ . collect ( Collectors . toList ()) ;
8087 }
8188 };
8289
83- // task.setOnRunning((e) -> loadingDialog.show());
8490 task .setOnSucceeded (event -> {
85- List <String > data = task .getValue ();
86- data .forEach (url -> masterData .add (new Image ( url ) ));
91+ List <Person > data = task .getValue ();
92+ data .forEach (p -> masterData .add (p ));
8793 pagination .setVisible (true );
8894 pagination .setPageCount (masterData .size () / PAGE_ITEMS_COUNT );
8995 });
9096
9197 Thread th = new Thread (task );
92-
9398 th .setDaemon (true );
94-
9599 th .start ();
96100 }
97101
0 commit comments