|
| 1 | +package com.baeldung.javafx.view; |
| 2 | + |
| 3 | + |
| 4 | +import com.baeldung.javafx.model.Person; |
| 5 | +import javafx.collections.FXCollections; |
| 6 | +import javafx.collections.ObservableList; |
| 7 | +import javafx.concurrent.Task; |
| 8 | +import javafx.fxml.FXML; |
| 9 | +import javafx.scene.Node; |
| 10 | +import javafx.scene.control.*; |
| 11 | +import javafx.scene.input.KeyCode; |
| 12 | +import javafx.scene.layout.VBox; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +public class SearchController { |
| 18 | + |
| 19 | + public static final int PAGE_ITEMS_COUNT = 10; |
| 20 | + |
| 21 | + @FXML |
| 22 | + private TextField searchField; |
| 23 | + @FXML |
| 24 | + private Button searchButton; |
| 25 | + @FXML |
| 26 | + private Pagination pagination; |
| 27 | + @FXML |
| 28 | + private Label searchLabel; |
| 29 | + |
| 30 | + private ObservableList<Person> masterData = FXCollections.observableArrayList(); |
| 31 | + |
| 32 | + public SearchController() { |
| 33 | + masterData.add(new Person(5, "John", true)); |
| 34 | + masterData.add(new Person(7, "Albert", true)); |
| 35 | + masterData.add(new Person(11, "Monica", false)); |
| 36 | + } |
| 37 | + |
| 38 | + @FXML |
| 39 | + private void initialize() { |
| 40 | + |
| 41 | + // search panel |
| 42 | + searchButton.setText("Search"); |
| 43 | + searchButton.setOnAction(event -> loadData()); |
| 44 | + searchButton.setStyle("-fx-background-color: #457ecd; -fx-text-fill: #ffffff;"); |
| 45 | + |
| 46 | + searchField.setOnKeyPressed(event -> { |
| 47 | + if (event.getCode().equals(KeyCode.ENTER)) { |
| 48 | + loadData(); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + searchField.textProperty().addListener((observable, oldValue, newValue) -> { |
| 53 | + searchLabel.setText(newValue); |
| 54 | + }); |
| 55 | + |
| 56 | + pagination.setPageFactory(SearchController.this::createPage); |
| 57 | + } |
| 58 | + |
| 59 | + private Node createPage(Integer pageIndex) { |
| 60 | + |
| 61 | + VBox dataContainer = new VBox(); |
| 62 | + |
| 63 | + TableView<Person> tableView = new TableView<>(masterData); |
| 64 | + TableColumn id = new TableColumn("ID"); |
| 65 | + TableColumn name = new TableColumn("NAME"); |
| 66 | + TableColumn employed = new TableColumn("EMPLOYED"); |
| 67 | + |
| 68 | + tableView.getColumns().addAll(id, name, employed); |
| 69 | + dataContainer.getChildren().add(tableView); |
| 70 | + |
| 71 | + return dataContainer; |
| 72 | + } |
| 73 | + |
| 74 | + private void loadData() { |
| 75 | + |
| 76 | + String searchText = searchField.getText(); |
| 77 | + |
| 78 | + Task<ObservableList<Person>> task = new Task<ObservableList<Person>>() { |
| 79 | + @Override |
| 80 | + protected ObservableList<Person> call() throws Exception { |
| 81 | + updateMessage("Loading data"); |
| 82 | + return FXCollections.observableArrayList(masterData |
| 83 | + .stream() |
| 84 | + .filter(value -> value.getName().toLowerCase().contains(searchText)) |
| 85 | + .collect(Collectors.toList())); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + task.setOnSucceeded(event -> { |
| 90 | + masterData = task.getValue(); |
| 91 | + pagination.setVisible(true); |
| 92 | + pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT); |
| 93 | + }); |
| 94 | + |
| 95 | + Thread th = new Thread(task); |
| 96 | + th.setDaemon(true); |
| 97 | + th.start(); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments