Skip to content

Commit 5f90412

Browse files
committed
JavaFX UI
1 parent c454749 commit 5f90412

5 files changed

Lines changed: 100 additions & 28 deletions

File tree

‎core-java/src/main/java/com/baeldung/javafx/Main.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public void start(Stage primaryStage) throws Exception {
1919
AnchorPane page = (AnchorPane) loader.load();
2020
Scene scene = new Scene(page);
2121

22+
scene.getStylesheets().add("/search.css");
23+
2224
primaryStage.setTitle("Title goes here");
2325
primaryStage.setScene(scene);
2426
primaryStage.show();
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+
}

‎core-java/src/main/java/com/baeldung/javafx/view/SearchController.java‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package com.baeldung.javafx.view;
22

33

4+
import com.baeldung.javafx.model.Person;
45
import javafx.collections.FXCollections;
56
import javafx.collections.ObservableList;
67
import javafx.concurrent.Task;
78
import javafx.fxml.FXML;
89
import 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.*;
1311
import javafx.scene.input.KeyCode;
1412
import javafx.scene.layout.VBox;
1513

16-
import java.util.ArrayList;
1714
import java.util.List;
15+
import java.util.stream.Collectors;
1816

1917
public 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

‎core-java/src/main/resources/SearchController.fxml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0"
1515
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
1616
<children>
17-
<Label text="Search Icons:"/>
17+
<Label text="Search Text:"/>
1818
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
1919
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
20+
<Label fx:id="searchLabel"/>
2021
</children>
2122
</HBox>
2223

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+
}

0 commit comments

Comments
 (0)