Skip to content

Commit a81bd92

Browse files
authored
Merge pull request eugenp#3894 from MherBaghinyan/BAEL-1638
Bael 1638
2 parents 2e1949b + 1a443a6 commit a81bd92

6 files changed

Lines changed: 228 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.javafx;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Scene;
6+
import javafx.scene.layout.AnchorPane;
7+
import javafx.stage.Stage;
8+
9+
public class Main extends Application {
10+
11+
public static void main(String[] args) {
12+
launch(args);
13+
}
14+
15+
@Override
16+
public void start(Stage primaryStage) throws Exception {
17+
18+
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/SearchController.fxml"));
19+
AnchorPane page = (AnchorPane) loader.load();
20+
Scene scene = new Scene(page);
21+
22+
scene.getStylesheets().add("/search.css");
23+
24+
primaryStage.setTitle("Title goes here");
25+
primaryStage.setScene(scene);
26+
primaryStage.show();
27+
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.javafx.model;
2+
3+
import javafx.beans.property.*;
4+
5+
public class Person {
6+
7+
private SimpleIntegerProperty id;
8+
private SimpleStringProperty name;
9+
private SimpleBooleanProperty isEmployed;
10+
11+
public Person(Integer id, String name, boolean isEmployed) {
12+
this.id = new SimpleIntegerProperty(id);
13+
this.name = new SimpleStringProperty(name);
14+
this.isEmployed = new SimpleBooleanProperty(isEmployed);
15+
}
16+
17+
public int getId() {
18+
return id.get();
19+
}
20+
21+
public IntegerProperty idProperty() {
22+
return id;
23+
}
24+
25+
public void setId(int id) {
26+
this.id.set(id);
27+
}
28+
29+
public String getName() {
30+
return name.get();
31+
}
32+
33+
public StringProperty nameProperty() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name.set(name);
39+
}
40+
41+
public boolean getIsEmployed() {
42+
return isEmployed.get();
43+
}
44+
45+
public BooleanProperty isEmployedProperty() {
46+
return isEmployed;
47+
}
48+
49+
public void setIsEmployed(boolean isEmployed) {
50+
this.isEmployed.set(isEmployed);
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Person{" +
56+
"id=" + id +
57+
", name='" + name + '\'' +
58+
", isEmployed=" + isEmployed +
59+
'}';
60+
}
61+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Button?>
4+
<?import javafx.scene.control.Label?>
5+
<?import javafx.scene.control.Pagination?>
6+
<?import javafx.scene.control.TextField?>
7+
<?import javafx.scene.layout.*?>
8+
<AnchorPane xmlns:fx="http://javafx.com/fxml"
9+
xmlns="http://javafx.com/javafx"
10+
fx:controller="com.baeldung.javafx.view.SearchController"
11+
prefHeight="400.0" prefWidth="600.0">
12+
<children>
13+
14+
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0"
15+
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
16+
<children>
17+
<Label text="Search Text:"/>
18+
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
19+
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
20+
<Label fx:id="searchLabel"/>
21+
</children>
22+
</HBox>
23+
24+
<Pagination fx:id="pagination"
25+
AnchorPane.leftAnchor="10.0"
26+
AnchorPane.rightAnchor="10.0"
27+
AnchorPane.topAnchor="50.0"
28+
visible="false">
29+
30+
</Pagination>
31+
32+
</children>
33+
</AnchorPane>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.button {
2+
-fx-background-color: red;
3+
-fx-text-fill: white;
4+
}

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
<module>persistence-modules/java-jdbi</module>
290290
<module>jersey</module>
291291
<module>java-spi</module>
292-
</modules>
292+
</modules>
293293

294294
<dependencies>
295295
<!-- logging -->

0 commit comments

Comments
 (0)